1

I have the following routing configuration under a MVC sample project:

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute("", "X{controller}/{action}",
            new { controller = "Customer", action = "List" });

        routes.MapRoute("MyRoute", "{controller}/{action}",
            new { controller = "Home", action = "Index" });
    }
}

I redirect all controllers (Home, Customer) to the same view that displays the Current controller and action name.

So, for the URL http://localhost:5O44O/XCustomer I have the following output:

The controller is: Customer
The action is: List

I expected that for the URL http://localhost:5O44O/X I should have the same output... But this is not the case...

Server Error in '/' Application.

The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /X

Why that? I placed the "X" condition first, so I should obtain the default replacements with Customer and List ?!

1 Answer 1

3

You a are receiving 404 error, because you don't have XController. If you have it, you'd receive route: http://localhost:5O44O/XX

routes.MapRoute("", "X{controller}/{action}" - this is just a syntaxis to generete string of route. And it doesn't have a behavior you were expected.

All manipulations should be done here:

new { controller = "Customer", action = "List" });

If you want to have such route: http://localhost:5O44O/X/List you need to write your MapRoute as follows:

routes.MapRoute("name", "X/{action}",
            new { controller = "Customer", action = "List" });

You may even write:

routes.MapRoute("name", "HelloBro",
                new { controller = "Customer", action = "List" });

It will return you route http://localhost:5O44O/HelloBro for your List action of Customer controller

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

5 Comments

I don't have the XController, nor I have the XCustomerController, but when I have an X the routing configuration should identify that case and take the default values for controller (Customer, in my case) and action (List, in my case). This is why, by eg. the link http://localhost:5O44O/XCustomer works well.
@Serge Route, that you have written expects controller name, because of {controller} statement. Try to change route as in my answer and you'll get, what you want.
that behavior is not very clear to me... The route I have written or expects the controller name, OR should use the default one I provided (Customer). What I don't understand is why this default controller is not used. Also, I don't want the X/BlaBla, I want XBlaBla (using the same routing segment)
And, I believe you have reason, I could use routes.MapRoute("name", "X", new { controller = "Customer", action = "List" }); this could make the desired stuff... but I just wonder why the route with default values does not work as expected....
@Serge I think that is because both your routes are expecting for Controller name. Runtime doesn't understand where to map, when you write just localhost:5O44O/X it tries to find "XController"

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.