简体   繁体   English

Web API项目路由麻烦

[英]Web api project routing trouble

I have simple web API project (ASP.NET MVC). 我有一个简单的Web API项目(ASP.NET MVC)。 I need to deploy my project in the subfolder on the IIS. 我需要将项目部署在IIS的子文件夹中。 For example, I have the site http://TestSite / and I need to deploy the project to http://TestSite/MyProject/ . 例如,我有站点http://TestSite /,并且需要将项目部署到http://TestSite/MyProject/ When I did it, web API routing stopped to work. 当我这样做时,Web API路由停止工作。 Because my ajax call is routed to the main site - http://TestSite/api/something/get . 因为我的ajax调用被路由到主站点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? 我做错了什么,在哪里可以了解ASP.NET MVC中Web API控制路由的一些做法?

It was pretty simple. 这很简单。 I didn't need to do anything with routing. 我不需要对路由做任何事情。 I just needed to change url in JavaScript. 我只需要更改JavaScript中的url。

From: 从:

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

To: 至:

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

Just remove '/' symbol before 'api'. 只需删除“ api”前的“ /”符号即可。 Be aware. 意识到。

BTW, nice article about debugging ASP.NET Web API - Debugging ASP.NET Web API with Route Debugger 顺便说一句,关于调试ASP.NET Web API的不错的文章- 使用Route Debugger调试ASP.NET Web API

EDIT: Forget that I wrote before (instead of the BTW section). 编辑:忘记我之前写的(而不是BTW部分)。 This doesn't work. 这行不通。 It works for http://TestSite/MyProject/ but doesn't work for http://TestSite/MyProject/MyController/Index . 它适用于http://TestSite/MyProject/但不适用于http://TestSite/MyProject/MyController/Index I met such kind of issue with my past ASP.NET MVC project. 我过去的ASP.NET MVC项目遇到了此类问题。 And I solved it by starting to use Url helper eg: @Url.Action("MyAction","MyController") . 我通过开始使用Url帮助程序解决了它,例如: @Url.Action("MyAction","MyController") So I needed something like for Web Api. 所以我需要Web Api之类的东西。 And I found it. 我找到了。 It is UrlHelper.HttpRouteUrl method. 它是UrlHelper.HttpRouteUrl方法。 Now my code looks in the next way: 现在,我的代码将以以下方式显示:

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

Where: 哪里:

  • DefaultApi - name of my default rote. DefaultApi-我的默认记号的名称。
  • Category - name of my api controller. 类别-我的api控制器的名称。

This solution doesn't look elegant but it works. 该解决方案看起来并不优雅,但可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM