ASP.NET MVC Routing: How do I make my MVC URL a .aspx extension?

I want the .aspx page urls in the MVC app, although there are no physical aspx pages and I will be using the Razor view engine.

1) Is it possible to define such a route?

2) What would this route look like if I wanted a URL like the one below:

http://example.com/controller/action.aspx

      

and optional

http://example.com/controller/action.aspx/id

      

and optional

http://example.com/controller/action.aspx?queryParam1=value&queryParam2=value

(etc...)

UPDATE

I understand that I need the url:

http://example.com/controller/id.aspx

      

In other words, I do not want specific actions to be omitted. The default action will process all requests.

ANOTHER UPDATE

What I have specified in my route config is the following:

routes.MapRoute(
name: "Default",
url: "{controller}/{id}.aspx",
defaults: new { controller = "Foo", action = "Index", id = "default" }
);

      

However, although the specified route works for Url where the Id is specified, for example the following:

http://example.com/foo/bar.aspx

      

Doesn't work if Id is not specified, for example, in the following example:

http://example.com/foo/

      

+3


source to share


1 answer


If all routes are affected, you can change the default route to look something like this:

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

      

If you only want to use specific routes, you can add an additional route as above, but with a different name than Default

, and leave the default route as it is. Then you can use the new route template via your name if needed.

Update:

I haven't tried this, so I'm not sure, but here's what I would like to try:

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

      



You will need to change the default value of the action parameter to match any action in your action.

Another update:

To handle this, I believe you will have to have two routes. The first route must have an ID and provide a default ID. If this route is not consistent, as in your second example, we'll drop 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 is first, and then you fall back to less specific routes, as your routes will be counted from top to bottom, and once a match is found, that route will be b.

+5


source







All Articles