这个问题涉及到ASP.NET MVC 5。
我试图通过使用路由属性在我的URL中使用连字符,但我没有任何运气让它工作。我在用这个问题和这个MSDN博客参考。这个我想要的URL是:
/my-test/
/my-test/do-something当我构建我的项目并在浏览器中测试的页面时,我会得到一个404错误。下面是我到目前为止掌握的代码:
// RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapMvcAttributeRoutes();
AreaRegistration.RegisterAllAreas();
...
}
}
// MyTestController.cs - NOTE: THIS FILE IS IN AN AREA
[RouteArea("MyTest")]
[RoutePrefix("my-test")]
[Route("{action=index}")]
public class MyTestController : Controller
{
[Route("~/do-something")]
public JsonResult DoSomething(string output)
{
return Json(new
{
Output = output
});
}
[Route]
public ViewResult Index()
{
return View();
}
}
// Index.cshtml
<script>
$(document).ready(function () {
// I'm using an ajax call to test the DoSomething() controller.
// div1 should display "Hello, World!"
$.ajax({
dataType: "json",
type: "POST",
url: "/product-management/do-something",
data: {
output: "Hello, World!"
}
}).done(function (response) {
$("div1").html(response.output);
});
});
</script>
<div id="div1"></div>当我创建该区域时,创建了一个区域注册文件,并且我在该文件中包含了所有的路由信息,但是根据MSDN博客,我可以删除该文件并完全依赖于路由属性。
发布于 2015-04-22 20:31:47
我想通了。该类需要以下RouteAreaAttribute:
[RouteArea("MyTest", AreaPrefix = "my-test")]
public class MyTestController : Controller
{
...
}这让我可以删除区域路线注册文件!
https://stackoverflow.com/questions/29804701
复制相似问题