简体   繁体   中英

Custom routing in asp.net mvc5

I would like to map the URL http://localhost:49930/upload -

RouteConfig.cs-

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" } 
        );
    }
}

Controller-

public class FileUploadController : Controller
{
    public ActionResult Index()
    {
        return View("ExcelUpload");
    }
}

But it's not working. Returns HTTP 404. Any help?

Routes get evaluated in order so ../upload matches your first ( Default ) route and attempts to call the Index() method of UploadController which does not exist, hence the 404 response.

Swap the routes so the ExcelUploader routes is before the Default route, and also remove the unnecessary segments/parameters

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM