这个问题与 ASP.NET MVC 5 有关。
我试图通过使用路由属性在我的 URL 中使用连字符,但我没有运气让它工作。 I'm using this question和 this MSDN blog reference.我想要的网址是:
/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 博客,我可以删除该文件并完全依赖路线属性。
最佳答案
我明白了。该类需要以下 RouteAreaAttribute:
[RouteArea("MyTest", AreaPrefix = "my-test")]
public class MyTestController : Controller
{
...
}
这也让我删除了区域路线注册文件!
关于c# - 在 ASP.NET MVC 5 中使用区域的路由属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29804701/