我如何可以结合MVC4网站,并在同一IIS网站下的WebAPI的网站? [英] How can I combine a MVC4 site and a WebAPI site under the same IIS website?

查看:226
本文介绍了我如何可以结合MVC4网站,并在同一IIS网站下的WebAPI的网站?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个项目(名为 Product.Api ),它有一个用于我们的WebAPI网站某些控制器。该控制器是在一个命名空间 Product.Api.Controllers 。该项目还具有其他的一些辅助性的东西为好。这被编译成库。

我也有另一个名为常规ASP.NET MVC4网站 Product.Web 。这有控制器(该命名空间下 Product.Web.Controllers ),模型和视图(在其相应的文件夹)。它也有一个的Global.asax.cs 拥有的路由信息​​。

在该网站在IIS中创建(.NET 4.5,IIS7,集成模式,等等),我复制 Product.Api DLL到bin文件夹。该 Product.Web 的主要场所。

现在我想的网址 http://www.product.com/api/ {控制器} / {显示}映射到的WebAPI的东西, http://www.product.com/ {控制器} / {行动} / {ID}映射到正规的MVC。

这是我现在作为我的路线:

  routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {控制器} / {ID},
                默认:新{n = RouteParameter.Optional}
            ).Constraints [命名空间] =新的String [] {Product.Api.Controllers};

            routes.MapRoute(
                名称:默认,
                网址:{控制器} / {行动} / {ID},
                默认:新{控制器=主页,行动=索引,ID = UrlParameter.Optional},
                命名空间:新的[] {Product.Web.Controllers}
            );
 

不幸的是,当我去到一个API URL(在这种情况下,例如是 HTTP:// WWW。 product.com/api/Tables/ (甚至当我定义一个id),我得到一个HTTP 404错误。

http://www.product.com/Home/Index 的正常工作。

奇怪的是我试过路由调试工具我仍然有问题。其实我从来没有看到调试器的输出。 http://www.product.com/Home/Index 和的 http://www.product.com/ 都表现的看法,但一切的API / *仍然显示404。我还没有看到任何输出调试器。

所以,反正是有得到预期的效果?

编辑:我发现我的global.asax.cs文件是在 Product.Web 命名空间。我改变了它是在产品命名空间,仍然没有去。

解决方案

  routes.MapHttpRoute(
                名称:DefaultApi
                routeTemplate:API / {控制器} / {ID},
                默认:新{n = RouteParameter.Optional}
            ).Constraints [命名空间] =新的String [] {Product.Api.Controllers};
 

这不会与MapHttpRoute工作。

如果你想使用ApiController路线从另一个垃圾桶,只是删除了限制,运行时会自动拿起你的ApiControllers从bin

。 MapHttpRoute不会与你的MVC控制器,反之亦然干扰,因此不会反正需要的约束。

或者用它来约束实现您的HttpRoute命名空间

  routes.DataTokens [命名空间] =新的String [] {myNameSpace对象};
 

或者使用<一个href="http://www.strathweb.com/2012/06/using-controllers-from-an-external-assembly-in-asp-net-web-api/"相对=nofollow>这个解决方案(但是这真的只是需要的,如果你保持的东西仓外。)

I have a project (named Product.Api) that has some controllers used for our WebAPI site. The controllers are in a namespace Product.Api.Controllers. The project also has some other helper stuff as well. This is compiled into a library.

I also have another regular ASP.NET MVC4 website named Product.Web. This has Controllers (under the namespace Product.Web.Controllers), Models and Views (in its corresponding folders). It also has a Global.asax.cs which has the routing information.

When the site is created in IIS (.NET 4.5, IIS7, integrated mode, etc), I copy the Product.Api dll into the bin folder. The Product.Web is the main site.

Now I want the url http://www.product.com/api/{Controller}/{id} to map to the WebAPI stuff and http://www.product.com/{Controller}/{Action}/{id} to map to the regular MVC.

This is what I have right now as my routes:

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}", 
                defaults: new {id = RouteParameter.Optional}
            ).Constraints["Namespaces"] = new string[] { "Product.Api.Controllers" };

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } ,
                namespaces: new[] { "Product.Web.Controllers" }
            );

Unfortunately when I go to an api url (in this case the example was http://www.product.com/api/Tables/ (or even when I define an id), I get a HTTP 404 error.

http://www.product.com/Home/Index works fine.

Oddly enough I tried the routing debugger tool and I still had issues. In fact I could never see the output of the debugger. http://www.product.com/Home/Index and http://www.product.com/ both show the view but everything api/* still shows 404. I have yet to see any output from the debugger.

So is there anyway to get the desired effect?

Edit: I noticed that my global.asax.cs file was in the Product.Web namespace. I changed it to be in the Product namespace and still no go.

解决方案

routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}", 
                defaults: new {id = RouteParameter.Optional}
            ).Constraints["Namespaces"] = new string[] { "Product.Api.Controllers" };

this will not work with MapHttpRoute.

If you want to use ApiController routes from the other bin, just remove the constraints, the runtime will automatically pick up your ApiControllers from the bin. MapHttpRoute will not interfere with your MVC controllers and vice versa, so the constraints are not needed anyway.

Alternatively use this to contraint your HttpRoute namespace

routes.DataTokens["Namespaces"] = new string[] {"MyNamespace"};

Or use this solution (but that's really only needed if you keep stuff outside of bin.)

这篇关于我如何可以结合MVC4网站,并在同一IIS网站下的WebAPI的网站?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆