1

I'm trying to wrap my mind around the way ASP.NET MVC implements routing.

From what is my current understanding, it seems my route string much have a "{controller}" and "{action}", otherwise it doesn't work?

How would I define the route that using a SearchController and Search action taking both SearchKeywords and SearchCaseSensitive arguments had the following URL?

domain/SearchKeywords/CaseSensitive

Even simpler, how do I map domain to controller SearchController and to Search?

3 Answers 3

2

From what is my current understanding, it seems my route string much have a "{controller}" and "{action}", otherwise it doesn't work?

Values for the controller and action tokens are required. You have 2 options for providing the values:

1) Using {controller} and {action} tokens on the URL template. e.g.:

routes.MapRoute(null, "{controller}/{action}");

2) Using default values for controller and action. e.g.:

routes.MapRoute(null, "some-url",
   new { controller = "Search", action = "Search" }
);

How would I define the route that using a SearchController and Search action taking both SearchKeywords and SearchCaseSensitive arguments had the following URL?

domain/SearchKeywords/CaseSensitive

The URL host (or domain) is not considered by the routing system, only the application relative path. You can do this:

routes.MapRoute(null, "{SearchKeywords}/{CaseSensitive}",
   new { controller = "Search", action = "Search" }
);

You can also provide defaults for SearchKeywords and CaseSensitive, if you want to make either of them optional.

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

Comments

2

You can add controller = "Search", action = "Search" to the defaults (the last parameter).

The routing engine will use values in defaults to fill in for parameters that aren't in the URL.

2 Comments

But that has the problem that if I want to add more routing rules, this one will "eat" them up. or not?
Also, how does it interpret domain/Abc? It will think Abc is the controller while I want it to be the string to search about!
0

If you want to have a 'domain' parameter in your route, you must put this at the top of the route registration. The 'domain' parameter in the second anonymous object is a constraint and here is set to be a regular expression that tests to see if the domain is either of the possible domains "DefaultDomain" or "OtherDomain".

        routes.MapRoute("DomainRoute", "{domain}/{controller}/{action}",
                        new {domain = "DefaultDomain", controller = "Search", action = "Search"},
                        new {domain = "DefaultDomain|OtherDomain"});

3 Comments

domain is not a parameter, its your site's domain
the routing doesn't take the domain into account
running domain/myargs will not call SearchController.Search(myargs)

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.