开发者

Route mappings are broken suddenly. How to fix?

开发者 https://www.devze.com 2023-04-07 03:15 出处:网络
Route mappings for my MVC app are broken suddenly. Here are route mappings public static void RegisterRoutes(RouteCollection routes)

Route mappings for my MVC app are broken suddenly.

Here are route mappings

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}", // URL with param开发者_运维问答eters
            new { controller = "Home", action = "Index" } // Parameter defaults
        );
    }

Here is controller code

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    public string Login()
    {
        return "Login";
    }
}

I can access Index view by using http://localhost:49637/ but I cannot access Login view via http://localhost:49637/Login. If I try to access Login view via http://localhost:49637/Home/Login, it works.


You have no route that matches - 'http://localhost:49637/Login' as your only route looks for both a controller and an action value, and you are only passing one value, so MVC is attempting to route you to a 'Login' controller that does not exist. I haven't tested this, but adding something like this should fix your problem-

routes.MapRoute(
    "Login",
    "Login",
    new { controller = "Home", action = "Login" }
);

Remember that asp.net MVC will use the first matching route it finds, so you'll need to place this route above the 'default' route in your RegisterRoutes function.

0

精彩评论

暂无评论...
验证码 换一张
取 消