Reputation: 77
We are slowly trying to migrate away from our classic asp pages to mvc views but I have come across an issue which I am struggling to figure out!
We want to keep the same url structure so I had created a controller/action to point to the same url, however this is being ignored and the asp file is always taking priority.
Example:
I have the folder structure: adults\policies\index.asp and an MVC controller called Adults with an action called Policies
When I browse to the url: ..co.uk\adults\policies
I expect the browser to go to my new controller action method but it is always going to the classic asp file location instead.
If I try to rename index.asp to something else I'll get a permissions error (so it is still attempting to use the classic asp path rather than the MVC controller action all the time).
I've spent hours trying to play around with routing but I can't seem to get it working. It's important that we keep the same url structure so I can't give my controller / action an alternative name to solve it.
Can anyone please help?
Thanks!
Edit:
I've sort of been able to get this to work using a rewrite module with some regex to then append the word "/index" on the end which then makes it go to the mvc view.
I don't know what pattern to use to ignore urls with an extension on the end though i.e. .asp / .jpeg etc. because the url is turning into /adults/policies/test.asp/index
So far I have included this pattern:
/?([^:/\s]+)((/\w+)/)([\w-.]+[^#?\s]+)(.)?(#[\w-]+)?$
Taken from here: http://www.regextester.com/20
Can anyone help so that this rewrite module won't kick in when there is a url with a .* on the end?
Upvotes: 1
Views: 665
Reputation: 95
according to this articel https://www.simple-talk.com/dotnet/asp.net/mixing-web-forms-and-asp.net-mvc/ you need to make sure that your MVC routing has a route defined for this controller/action.
Therefore make sure that you don't have a rule like this defined in your RegisterRoutes(RouteCollection routes)
function inside of your RouteConfig.cs file that will ignore the route.
//Do not route requests for /adults/policies/any/additional/path
routes.IgnoreRoute("adults/policies/");
//Do not route for adults/policies/any/additional/path?withquerystring=true
routes.IgnoreRoute("adults/policies/{*pathInfo}");
also, make sure you have somthing like this in your. RegisterRoutes(RouteCollection routes)
function inside of your RouteConfig.cs file
routes.MapRoute(
"Adult Policies",
"adults\policies\index.asp"
new { controller = "MyController", action = "MyAction"}
);
Unfortunately it will be very difficult if not impossible to create a route that will catch all of the .asp pages that you want to route in this way since, as you mentioned in your comments, there are other asp files in that folder that you still want to serve up. This means that you are going to have to create a route like this every time you replace one of these pages, up until the point where you have replaced all of the pages, in which case a catch all for that controller is simple.
The other option is creating a very specific ignore rule for all of the current asp pages in that folder and removing them one by one as you convert them to actions. And of course having a very broad route defined that will catch all of the request to that folder.
Upvotes: 1