【发布时间】:2016-01-06 15:29:27
【问题描述】:
这是一篇很长的文章。我正在使用此处描述的属性路由: 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 控制器:
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(甚至在服务器 localhost 上测试: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 控件可以在服务器和本地完美运行。
问题:可能出了什么问题,我应该从哪里开始寻找?!
【问题讨论】:
-
可能是您的 HelloController 没有进入网站。此外,如果这真的是您的代码“原样”,那么它将无法编译。 GetTest() 的返回值是一个字符串,但您返回的是整数 1。 - 或者这可能是 C# 自动转换的新改进之一。
-
我的错误。已更正。我应该如何检查控制器是否正在访问该站点?
-
一切都取决于您的网站是如何托管的。如果它是您的服务器或您可以远程访问的东西(远程桌面、VNC ...),那么您只需查看文件系统即可。假设您要发布到 Azure,您可以查看发布日志或输出窗口以确保文件已被复制 - 但请先进行更改,以便它知道需要更新它。
-
@Mike 实际上我可以远程访问已发布的站点,即使我在 localhost 上尝试也出现错误。我认为控制器不是要查找的文件夹或文件,它位于 dll 中,因此很难查看。我已删除所有 dll,重新发布,同样的错误。我认为该错误可能与服务器上缺少的软件/dll 有关。
标签: c# asp.net asp.net-mvc asp.net-web-api