根据这篇文章,尝试在我的 MVC 应用程序中使用 [Route] 属性: https ://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5 /
[Route("Error/{type?}")]
public ActionResult Error(string type)
{
if (type == "Duplicate")
{
ViewBag.ErrorDetails = "There is already a redirect using that Vanity URL and Domain.";
}
if (type == "Unknown")
{
ViewBag.ErrorDetails = "An unknown error has occured. Probably your fault too.";
}
return View();
}
我在 RouteConfig.cs 中有 MapMvcAttributeRoutes
public class RouteConfig
{
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 }
);
routes.MapMvcAttributeRoutes();
}
}
浏览到 /Admin/Error/Duplicate 但 /Admin/Error?type=Duplicate 工作正常时,我得到 404。
除了[Route("Error/{type}")]
我尝试过[Route("Admin/Error/{type})]
,[Route("{type})]
但没有运气。我不知道我哪里错了。