简体   繁体   English

未找到与请求 URI 匹配的 HTTP 资源,我需要解决方案

[英]No HTTP resource was found that matches the request URI, I need solutions

I got an error when trying to test my web api url尝试测试我的 web api url 时出错

This is my global.asax:这是我的 global.asax:

void Application_Start(object sender, EventArgs e)
        {
            RouteTable.Routes.MapHttpRoute(
               name: "DefaultApi",
               routeTemplate: "api/{controller}/{id}",
               defaults: new { id = System.Web.Http.RouteParameter.Optional }
            );
        }

Here is my controller:这是我的控制器:

public class TestController : ApiController
    {
        [Route("test")]
        [HttpPost]
        public PaymentResponseModel Response()
        {
            log.Info("Hello world");

            PaymentResponseModel prm = new PaymentResponseModel();
            prm.info1 = "tes";
            return prm;
        }
     }

This is the error message that i got:这是我收到的错误消息:

{
    "Message": "No HTTP resource was found that matches the request URI 'http://localhost:49484/api/test'.",
    "MessageDetail": "No route providing a controller name was found to match request URI 'http://localhost:49484/api/test'"
}

This is my url that i tested:这是我测试的网址:

http://localhost:49484/api/test

You have not mentioned action in routing.您没有提到路由中的操作。 Remove [Route("test")] .删除[Route("test")]

Try below code.试试下面的代码。

void Application_Start(object sender, EventArgs e)
    {
        RouteTable.Routes.MapHttpRoute(
           name: "DefaultApi",
           routeTemplate: "api/{controller}/{id}",
           defaults: new { action = "Response", 
                           id = System.Web.Http.RouteParameter.Optional }
        );
    }

If you want to add route path on action level use MapHttpAttributeRoutes();如果要在操作级别添加route path ,请使用MapHttpAttributeRoutes();

You need to have action in your routeTemplate.您需要在您的 routeTemplate 中进行操作。 You can do this:你可以这样做:

void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "api/{controller}/{action}/{id}",
       defaults: new { action = "Response", 
                       id = System.Web.Http.RouteParameter.Optional }
        );
 }

and test it like:并测试它:

http://localhost:49484/api/test

Or you can also do或者你也可以这样做

void Application_Start(object sender, EventArgs e)
{
    RouteTable.Routes.MapHttpRoute(
       name: "DefaultApi",
       routeTemplate: "api/{controller}/{action}/{id}",
       defaults: new { id = System.Web.Http.RouteParameter.Optional }
        );
 }

and test it like:并测试它:

http://localhost:49484/api/test/Response

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

相关问题 我收到错误“没有找到与请求 URI 匹配的 HTTP 资源” - I am getting error “No HTTP resource was found that matches the request URI” 未找到与Angular 2中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in Angular 2 找不到与请求URI'MyUrl'匹配的HTTP资源 - No HTTP resource was found that matches the request URI 'MyUrl' 获取没有与请求URI匹配的HTTP资源 - Getting No HTTP resource was found that matches the request URI 找不到与请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI 找不到与webapi中的请求URI匹配的HTTP资源 - No HTTP resource was found that matches the request URI in webapi 未找到与请求URI匹配的HTTP资源,未找到与控制器匹配的类型 - No HTTP resource was found that matches the request URI, No type was found that matches the controller 路由错误:找不到与请求URI匹配的HTTP资源 - Routing error: No HTTP resource was found that matches the request URI 自定义“找不到与请求uri匹配的http资源”消息 - Customizing “No http resource was found that matches the request uri” message 为什么这里“找不到与请求 URI 匹配的 HTTP 资源”? - Why is it that "No HTTP resource was found that matches the request URI" here?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM