路由属性不起作用

时间:2017-09-14 21:38:56

标签: c# asp.net asp.net-mvc routes routeattribute

根据本文,尝试在我的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时,我得到404?type = Duplicate正常工作。

除了[Route("Error/{type}")]我还试过[Route("Admin/Error/{type})][Route("{type})]但没有运气。我无法弄清楚我哪里出错了。

0 个答案:

没有答案