gpt4 book ai didi

c# - 属性路由不起作用 MVC5

转载 作者:行者123 更新时间:2023-12-04 05:36:15 25 4
gpt4 key购买 nike

这是一篇很长的文章。我正在使用此处描述的属性路由: http://blogs.msdn.com/b/webdev/archive/2013/10/17/attribute-routing-in-asp-net-mvc-5.aspx#enabling-attribute-routing

我在 WebApiConfig.cs 中放置了:

public static void Register(HttpConfiguration config)
{
config.EnableCors();
+ config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
}

在 Global.asax.cs 中

             AreaRegistration.RegisterAllAreas();
+ //WebApiConfig.Register(GlobalConfiguration.Configuration);
+ GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);

我正在使用一个 webapi Controller :

public class HelloController : ApiController
{
[Route("Services/test/Application/{id}")]
public string GetTest(int id)
{
return "1";
}
}

我正在使用 Postman Chrome 扩展程序进行测试。在我自己的计算机上,当我在 Visual Studio 中进行测试时,它运行良好:http://localhost:6296/Services/test/Application/12并返回预期结果,但在我将其部署到站点后,它不起作用:http://www.mytest.com/Services/test/Application/12 (甚至在服务器本地主机上测试:http://localhost/Services/test/Application/12)并返回:

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /Services/test/Application/12

引用System.Web.Mvc(版本5.2.3.0)被标记为“copy local = true”。没有使用授权。经典的 webapi 控件可以在服务器和本地完美运行。

问题:可能出了什么问题,我应该从哪里开始寻找?!

最佳答案

将此添加到您的 WebApiConfig.cs

public static void RegisterRoutes(RouteCollection routes)
{

routes.MapMvcAttributeRoutes();
}

关于c# - 属性路由不起作用 MVC5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33025443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com