I'm currently a little new to Microsoft's MVC4 and I don't quite understand routing as well as I would like.
What I am trying to do is make my URLs more human readable. Currently I have URLs that look like this:
- foo.com/UserProfile/Details/6
- foo.com/UserExperience/Details/
- foo.com/UserSpecificController/Edit/8
All of the user controllers are prefixed with "User" I was wondering if it was possible to change those URLs so they look like this:
- foo.com/u/Profile/Details/6
- foo.com/u/Experience/Details/
- foo.com/u/SpecificController/Edit/8
My first attempt was with IIS:
<rewrite>
<rules>
<rule name="AddTrailingSlashRule1" stopProcessing="true">
<match url="(.*[^/])$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
</conditions>
<action type="Redirect" url="{R:1}/" />
</rule>
<rule name="Pretty User Redirect" enabled="false" stopProcessing="true">
<match url="User(.*)/(.*)" />
<action type="Redirect" url="u/{R:1}/{R:2}" />
<conditions>
</conditions>
</rule>
<rule name="User pretty URL Rewrite">
<match url="u/(.*)/(.*)" />
<action type="Rewrite" url="User{R:1}/{R:2}" />
</rule>
</rules>
</rewrite>
This worked quite well except I would get /u/ on all of my links, everywhere...
For example: foo.com/Home/WhatWeDo/
would come out like: foo.com/u/Home/WhatWeDo/
This would then 404.
I am using the default routing configuration
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
And all of my links are drawn with @Html.ActionLink(...)
If anyone could shed some light on this, that would be greatly appreciated.