0

我的路线配置看起来像这样

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

我将用户控制器按操作划分为单独的文件,因此所有GET操作都在GetUserController.cs文件中,PUT操作在PutUserController.cs等等......

GET文件有一个像这样的部分类

[RoutePrefix("api/users/{userId:Guid}/locations")]
public partial class UsersController : MyCustomApiController
{    
    [Route("{locationId:Guid}/list")]
    [HttpPost]
    public async Task<IHttpActionResult> GetUsers(Guid userId, Guid locationId, [FromBody] Contracts.UserRequest request)
    { 
    }
}

PUT文件有一个像这样的部分类

public partial class UsersController : MyCustomApiController
{    
    [Route("{locationId:Guid}/insert")]
    [HttpPut]
    public async Task<IHttpActionResult> InsertUser(Guid userId, Guid locationId, [FromBody] Contracts.UserRequest request)
    { 
    }
}

不管我做什么,我总是得到一个 404 错误。我正在使用 Postman 进行测试我正在使用Content-Typeapplication/json URL 是使用 http://localhost:52450/api/users/3F3E0740-1BCB-413A-93E9-4C9290CB2C22/locations/4F3E0740-1BCB-413A-93E9-4C9290CB2C22/lista ,POST因为我无法使用GET第一种方法发布复杂类型

http://localhost:52450/api/users/3F3E0740-1BCB-413A-93E9-4C9290CB2C22/locations/4F3E0740-1BCB-413A-93E9-4C9290CB2C22/insert有一个PUT

如果有的话,我还需要在路由配置中设置哪些其他路由?

编辑

奇怪的是,另一个也是部分类的控制器似乎使用相同的配置

[RoutePrefix("api/products")]
public partial class ProductController : MyCustomApiController
{

    [Route("insert")]
    [HttpPut]
    public async Task<IHttpActionResult> InsertProduct([FromBody] InsertProductRequest request)
    {
    }
}

这是连接一切的global.asax.cs 。

protected void Application_Start(object sender, EventArgs e)
{

    AreaRegistration.RegisterAllAreas();

    GlobalConfiguration.Configure(WebApiConfig.Register);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    FilterConfig.RegisterFilters(GlobalFilters.Filters);

    // Only allow Tls1.2!
    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;


}
4

2 回答 2

0

那是配置 Web API 路由的错误配置文件。

//global.asax.cs
GlobalConfiguration.Configure(WebApiConfig.Register); // <-- WEB API
RouteConfig.RegisterRoutes(RouteTable.Routes); // <-- MVC

检查WebApiConfig.cs以配置 Web API 路由。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Attribute routing.
        config.MapHttpAttributeRoutes();

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

此外,您的路线限制Guid是错误的。它应该是{parameterName:guid}

[RoutePrefix("api/users/{userId:guid}/locations")]
public partial class UsersController : MyCustomApiController { 
    [HttpPost]
    [Route("{locationId:guid}/list")]
    public async Task<IHttpActionResult> GetUsers(Guid userId, Guid locationId, [FromBody] Contracts.UserRequest request) { ... }
}

来源:ASP.NET Web API 2 中的属性路由

于 2017-02-28T18:30:27.117 回答
0

您可以尝试在 WebApiConfig 中这样做:

  config.MapHttpAttributeRoutes();
  var corsAttr = new EnableCorsAttribute("*", "*", "*");
  config.EnableCors(corsAttr);
于 2017-02-28T18:35:55.173 回答