asp.net mvc5中的自定义路由

时间:2016-12-11 09:54:16

标签: c# asp.net-mvc routing

我想映射网址melt(setDT(mydf), measure = patterns("ID", "string"), na.rm=TRUE, value.name = c("Ids", "string"))[!duplicated(Ids, fromLast=TRUE) ][, variable := NULL][order(Ids)] -

RouteConfig.cs -

http://localhost:49930/upload

控制器 -

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 },
             namespaces: new[] { "BrSupervisorTracker.Controllers" }
        );

        routes.MapRoute(
            name: "ExcelUploader",
            url: "upload/{controller}/{action}/{id}",             
            defaults: new { controller = "FileUpload", action = "Index", id = UrlParameter.Optional },
             namespaces: new[] { "BrSupervisorTracker.Controllers" } 
        );
    }
}

但它不起作用。返回HTTP 404.任何帮助?

1 个答案:

答案 0 :(得分:1)

路由按顺序进行评估,因此../upload与您的第一个(Default)路由匹配,并尝试调用Index() UploadController不存在的404方法,因此{ {1}}回复。

交换路由以使ExcelUploader路由位于Default路由之前,并删除不必要的段/参数

routes.MapRoute(
    name: "ExcelUploader",
    url: "upload",
    defaults: new { controller = "FileUpload", action = "Index"},
    namespaces: new[] { "BrSupervisorTracker.Controllers" } 
);
routes.MapRoute(
    name: "Default",
    ....
};