views:

1100

answers:

5

How to hide controller name in Url?

I use the ASP.NET MVC.

The original url is: http://www.sample.com/Users.mvc/UserDetail/9615

The "Users" is controller name, the "UserDetail" is action name, and the "9615" is UserId.

How can I hide the controller name and action name in the url.

Just like this: http://www.sample.com/9615

I have writed the following code in the Global.ascx.cs to hide the action name:

routes.MapRoute(
             "UserDetail",             // Route name
             "Users.mvc/{UserId}",              // URL with parameters
             new { controller = "Users", action = "UserDetail", UserId = "" }  // Parameter defaults
            );

Using the above code I hid the action name and got this url: http://www.sample.com/Users.mvc/9615

But how can I hide the controller name and get this url: http://www.sample.com/9615

Thanks.

A: 

But the UserId can be any string not just numbers, How can I set a regex to match that?

Mike108
@Mike108, Hi mike, welcome to StackOverflow. Rather than reply with an "Answer" like this.. I would suggest making posts like this "comments" on the answer in question. That way this space is reserved for answers only.
Simucal
+2  A: 

The idea is the same. You do just the thing you did to the action. However, your problem arises from the fact that IIS is probably not mapping www.xyz.com/1234 to ASP.NET runtime. To do so in IIS7, enable integrated mode and in IIS6, add a wildcard mapping in handler map that maps everything to ASP.NET.

To add a wildcard map, see http://haacked.com/archive/2008/11/26/asp.net-mvc-on-iis-6-walkthrough.aspx (Search for "IIS6 Extension-less URLs" in that page)

After that, simply add a route:

routes.MapRoute("UserDetails", "{UserID}/{*name}", 
    new { controller = "Users", action = "UserDetail" , UserID=""});

This should do the trick.

Mehrdad Afshari
A: 

Can you explain more detail about how to do wildcard mapping in IIS 6 ?

How to add a wildcard mapping in handler map that maps everything to ASP.NET in IIS 6 ?

Mike108
A: 

Thank you. It works.

But what is the meaning of "{name}"" in the "{UserID}/{name}" ?

I just use "{UserID}", and it works.

routes.MapRoute("UserDetails", "{UserID}", new { controller = "Users", action = "UserDetail", id="" });
Mike108
A: 

what to do in case if we want http://www.abc.com.au http://abc.com.au www.abc.com.au http://www.abc.com.au/home to point to the same location. i.e. same controller as they all point to the same content.