1

I have simple web API project (ASP.NET MVC). I need to deploy my project in the subfolder on the IIS. For example, I have the site http://TestSite/ and I need to deploy the project to http://TestSite/MyProject/. When I did it, web API routing stopped to work. Because my ajax call is routed to the main site - http://TestSite/api/something/get.

I have tried to update map routing in the next way:

routeTemplate: "MyProject/api/{controller}/{id}",

But it doesn't affect as I want. What I am doing wrong and where can I read about some practices of web API control routing in ASP.NET MVC?

4

1 回答 1

2

这很简单。我不需要对路由做任何事情。我只需要更改 JavaScript 中的 url。

从:

$.getJSON('/api/Category', function (data) {

至:

$.getJSON('api/Category', function (data) {

只需删除 'api' 之前的 '/' 符号。意识到。

顺便说一句,关于调试 ASP.NET Web API 的好文章 -使用 Route Debugger 调试 ASP.NET Web API

编辑:忘记我之前写的(而不是 BTW 部分)。这行不通。它适用于http://TestSite/MyProject/但不适用于http://TestSite/MyProject/MyController/Index. 我在过去的 ASP.NET MVC 项目中遇到了此类问题。我通过开始使用Url helper解决了这个问题,例如:@Url.Action("MyAction","MyController"). 所以我需要类似 Web Api 的东西。我找到了。它是UrlHelper.HttpRouteUrl方法。现在我的代码如下:

$.getJSON( "@Url.HttpRouteUrl("DefaultApi",new {controller = "Category"})", function (data) {

在哪里:

  • DefaultApi - 我的默认死记硬背的名称。
  • 类别 - 我的 api 控制器的名称。

这个解决方案看起来并不优雅,但它确实有效。

于 2013-04-24T08:58:24.073 回答