asp.net-mvc 未找到与 Web API 中的请求 URI 匹配的 HTTP 资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15715744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:42:58  来源:igfitidea点击:

No HTTP resource was found that matches the request URI in Web API

asp.net-mvcasp.net-web-api

提问by DharaPPatel

I have configured my WebApiConfig like this:

我已经像这样配置了我的 WebApiConfig:

public static void Register(HttpConfiguration config)
{
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{controller}/{action}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );
}

I have one method which accepts one parameter. The accessing URI is http://localhost:8598/api/WebApi/GetLocationCategory/87.

我有一种方法接受一个参数。访问 URI 是http://localhost:8598/api/WebApi/GetLocationCategory/87.

This gives me an error: No HTTP resource was found that matches the request URI 'http://localhost:8598/api/WebApi/GetLocationCategory/87'

这给了我一个错误: No HTTP resource was found that matches the request URI 'http://localhost:8598/api/WebApi/GetLocationCategory/87'

Controller:

控制器:

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int CatID)
{
    var LocCats = (from lct in entities.tdp_LocationCategories join lc in entities.tdp_LocationMaster on lct.FK_LocationID equals lc.LocationID where lct.IsApproved == 0 && lct.FK_CategoryID == CatID select new { lc.LocationID, lc.LocationName }).ToList();
    List<LocationCategory_CLS> loc = new List<LocationCategory_CLS>();

    foreach (var element in LocCats)
    {
        loc.Add(new LocationCategory_CLS
        {
            LocationID = element.LocationID,
            LocationName = element.LocationName
        });
    }
    return loc;
}

回答by Shubh

Try changing your Controller method as

尝试将您的控制器方法更改为

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int id) <-- Change
{
    var LocCats = (from lct in entities.tdp_LocationCategories join lc in entities.tdp_LocationMaster on lct.FK_LocationID equals lc.LocationID where lct.IsApproved == 0 && lct.FK_CategoryID == id select new { lc.LocationID, lc.LocationName }).ToList();
    List<LocationCategory_CLS> loc = new List<LocationCategory_CLS>();

    foreach (var element in LocCats)
    {
        loc.Add(new LocationCategory_CLS
        {
            LocationID = element.LocationID,
            LocationName = element.LocationName
        });
    }
    return loc;
}

The change is only, changing input parameter from CatIdto id.... It works for me many times..

更改只是将输入参数从更改CatIdid.... 它对我有用很多次..

Edit:

编辑



Its a long time when I look back I thinkI know the reason now. Words Like Jaredis correct, it's all to do with Routing which we specify. If I have a route(default) as :

久久回首,我想我现在知道原因了。Words Like Jared是正确的,这完全与我们指定的路由有关。如果我有一条路线(默认):

routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );

And my URL is /MyController/GetLocationCategory/123, it will be equivalent to /MyController/GetLocationCategory?id=123.

而我的 URL 是/MyController/GetLocationCategory/123,它将相当于/MyController/GetLocationCategory?id=123.

Similarly, if I want to change my parameter name for Id to say CatId, then I need to change the query string parameter(the way I am calling my Controller Action would change). Which would now be :

同样,如果我想将 Id 的参数名称更改为 CatId,那么我需要更改查询字符串参数(我调用控制器操作的方式会改变)。现在是:

/MyController/GetLocationCategory?CatId=123

/MyController/GetLocationCategory?CatId=123

回答by P.Brian.Mackey

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

Maps to:

映射到:

http://localhost:8598/api/Controller/Action/id

http://localhost:8598/api/Controller/Action/id

The WebApiportion of the url is redundant with api. Then modify the method parameter name to match the route:

WebApiurl的部分是多余的api。然后修改方法参数名称以匹配路由:

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int id)

public IEnumerable<LocationCategory_CLS> GetLocationCategory(int id)

This is a good default as it matches the convention.

这是一个很好的默认值,因为它符合约定。

Alternatively, you can modify the route to use this unconventional parameter name instead:

或者,您可以修改路由以使用此非常规参数名称:

config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{action}/{CatID}",
            defaults: new { CatID = RouteParameter.Optional }
        );

Finally, in either case make sure the controller name ends in Controller.

最后,无论哪种情况,请确保控制器名称以Controller.

Good names: CustomController, CustomApiController
Bad names: Custom, CustomApi

好名字:CustomControllerCustomApiController
坏名字:CustomCustomApi

回答by ashutosh raina

what is the name of your controller and action ?

您的控制器和操作的名称是什么?

The URL should be

网址应该是

http://localhost:8598/api/Controller/Action

It does not map to the Route Configuration you have specified, hence the pipeline is unable to locate the correct Controller. The /id should not be in the path it must be in the body or the query parameters ( I got stumped in haste !!)

它没有映射到您指定的路由配置,因此管道无法找到正确的控制器。/id 不应该在它必须在正文或查询参数中的路径中(我被仓促难住了!!)

Example : -

例子 : -

public class FooController : ApiController
    {
        public int GetIndex(int id)
        {
            return id;
        }
    }

localhost:58432/api/foo/GetIndex?Id=1

O/P :- 1

Note:- If your action name is GetIndexthen the URL must be GetIndexnot just Index. Optionally you can specify the [HttpGet]attribute for the action.

注意:- 如果您的操作名称是,GetIndex那么 URLGetIndex不能只是索引。您可以选择指定操作的[HttpGet]属性。

If you have the following as well in your WebApi.config

如果您的 WebApi.config 中也有以下内容

config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

Then http://localhost:58432/api/foo?Id=1would also be a legitimate route. Not sure if you would want that.

然后http://localhost:58432/api/foo?Id=1也将是一个合法的路线。不确定你是否想要那个。

Also, With WebAPI as far as possible stick to non action based routing , MVC is meant for that. It is not the recommended way.

此外,使用 WebAPI 尽可能坚持基于非动作的路由,MVC 就是为此而生的。这不是推荐的方式。

回答by JTech

Your request URL is http://localhost:8598/api/WebApi/GetLocationCategory/87

您的请求网址是 http://localhost:8598/api/WebApi/GetLocationCategory/87

Your route is configured to accept: 'api/{controller}/{action}/{id}'

您的路由配置为接受:'api/{controller}/{action}/{id}'

So you need to ensure that the name of your controller is 'WebApiController'.

因此,您需要确保控制器的名称是“WebApiController”。

Also, as stated alredy by @SDG you need to make sure that the name of the parameter to your action method matches what you have in your Route template i.e change 'CatID' to 'id'

此外,正如@SDG 所述,您需要确保操作方法的参数名称与您在路由模板中的名称相匹配,即将“CatID”更改为“id”