I want an MVC app to have .aspx page URLs even though there will not be any physical aspx pages and I will be using the Razor view engine.
1) Is it possible to define such a route?
2) What would that route look like if I wanted a url such as, say, the one given below:
http://example.com/controller/action.aspx
and optionally
http://example.com/controller/action.aspx/id
and optionally
http://example.com/controller/action.aspx?queryParam1=value&queryParam2=value
(and so on...)
UPDATE
I realize I want URL's like this:
http://example.com/controller/id.aspx
In other words, I want no specific action to be specified. A default action will process all requests.
ANOTHER UPDATE
What I have specified in my route config is this:
routes.MapRoute(
name: "Default",
url: "{controller}/{id}.aspx",
defaults: new { controller = "Foo", action = "Index", id = "default" }
);
However, while the above route does work for Url's where Id is specified such as the ones below:
http://example.com/foo/bar.aspx
It does not work when no Id is specified, such as in the case below:
http://example.com/foo/
If it should affect all routes, you could alter the default route to look something like this:
routes.MapRoute(
"Default",
"{controller}/{action}.aspx/{id}",
new { controller = "Home", action = "Index" }
);
If you only want it for particular routes, than you could add an additional route like the one above, but with another name than Default
, and leave the default-route as it is. You could then use the new route-pattern through its name when needed.
Update:
I haven't tried this, so I'm not certain, but this is what I would try:
routes.MapRoute(
"Default",
"{controller}/{id}.aspx",
new { controller = "Home", action = "Index" }
);
You would have to modify the default-value of the action-parameter, so that it matches whatever the of your action is.
Another update:
To handle that, I believe you will have to have two routes. The first route should require an ID and specify no default id. If that route isn't matched, like in your second example, we will fall down to the second route:
routes.MapRoute(
"DefaultWithId",
"{controller}/{id}.aspx",
new { controller = "Home", action = "Index" }
);
routes.MapRoute(
"Default",
"{controller}/",
new { controller = "Home", action = "Index" }
);
It is important that the most specific route comes first, and that you then fall back to less and less specific routes, since your routes will be read top-down, and as soon as a match is found, that route will be used.