1

I have the following routings in my RoutingConfig.cs

        routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

        // Not working why?
        routes.MapRoute(
            name: "AdminLoginRequestUrl",
            url: "{controller}/{action}/{requestUrl}",
            defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
        );

The problem is that the second routing is not working What do i miss here? does someone has any tips or Ideas MVC is abit new for me

3 Answers 3

1

You cannot create different route only by differentiating parameter name, both route present similar one. Also move your custom route above default one. You can try this

    routes.MapRoute(
        name: "AdminLoginRequestUrl",
        url: "{controller}/{action}/route2/{requestUrl}",
        defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
    );

   routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

you can use anything insted of route2 to differentiate from default route

Sign up to request clarification or add additional context in comments.

8 Comments

What has route2 has to be ? sorry i'm new to MVC
anything you want to use to differentiate from the default one
just need to make some difference between them
What if i already have 2 params than i need to add a thirth>>
no its not param it will be part of the route like admin/login/route2/{yourparam}
|
0

You cannot differ your routs by variable names. You can create more specific route like this:

    routes.MapRoute(
        name: "AdminLoginRequestUrl",
        url: "Admin/{action}/{requestUrl}",
        defaults: new { controller = "Admin", action = "Login", requestUrl= UrlParameter.Optional }
    );

routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );

Note that the order of registration is significant.

Or you can rename your parameters to something that will be appropriate for both controllers and use that.

Comments

0

Consider using MapHttpAttributeRoutes(), you can read about it here: https://blogs.msdn.microsoft.com/webdev/2013/10/17/attribute-routing-in-asp-net-mvc-5/1.

Edit: Changed link for better article.

Then you can use attributes to define more specific rules on your actions.

Example:

public class Admin : Controller
{
    [Route("admin/{requestUrl}")]
    public ActionResult Login(string requestUrl) { ... }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.