URL rewrite for .Net MVC Application - asp.net-mvc

I have .Net MVC 5 application currently running with url like http://www.example.com in production (Hosted in IIS on Windows server). Now we have requirement to change the URL path to like this http://www.example.com/app.
What will be the best approach to achieve this?

The best way is to create a subdirectory app in the root directory of your website and move your .Net MVC 5 application into the app folder, if you set the Default Document, you can directly access it via http://www .example.com/app to access.
In addition, if you just want to access the content of http://www.example.com by typing http://www.example.com/app in your browser, you can try the following rewrite rule:
<rewrite>
<rules>
<rule name="test">
<match url="app" />
<action type="Rewrite" url="http://www.example.com/" />
</rule>
</rules>
</rewrite>

Related

301 Redirect Classic ASP requests to ASP.NET MVC 5

I've been given the task of figuring out how to handle 301 redirects for a Classic ASP site to a new ASP.NET MVC 5 application. The domain name will remain the same.
Example of what I need to do:
Old Url:
http://www.example.com/index_cityname.asp
New Url (MVC):
http://www.example.com/cityname
The system will most likely be using IIS 8.x. I'm developing on a Windows 8.1 machine and I've enabled ASP in the Features. But when I enter an .asp extension in the browser, I get a 404.
I built a custom component that analyzes the incoming request and then checks a list and if it exists, it maps the new Response.RedirectPermanent(newPath, true) to the new path. The only problem is, it never gets hit. Somewhere earlier in the lifecycle the application sees that it's an .asp request, and exits.
Any ideas on how to make 301 redirects work from Classic ASP to ASP.NET MVC 5 using the same domain name?
Try adding the following to your web.config file:
<modules runAllManagedModulesForAllRequests="true" />
It belongs under system.webServer and configuration.
But beware performance and other issues using this setting. See http://www.britishdeveloper.co.uk/2010/06/dont-use-modules-runallmanagedmodulesfo.html
You can redirect directly by using rules in web.config. I tried to write up your use case, but maybe you have to tweak the matching rule or target action to suit your needs.
Add this to your web.config:
<configuration>
<system.webServer>
<rewrite>
<rules>
<rule name="redirect classic asp" stopProcessing="true">
<match url="index_(\w+)\.asp$" />
<action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>
</rules>
</rewrite>
</system.webServer>
</configuration>
I can't go into all details of web.config rewrite rules, but the best (easy) documentation I could find is here: Learn.IIS Rewrite Module
There is also a reference here: Rewrite Module Reference

Azure web site - URL Rewrite using web.config not working

We are migrating some sites over to Azure and I have it setup as a "Web Site".
An example of what we're trying to do is pretty straight forward.
Current URL: http://www.website.com/Products.aspx
Clean URL: http://www.website.com/Products/
So in the web.config, there is the following:
<system.webServer>
<rewrite>
<rules>
<rule name="ProductList">
<match url="/Products.aspx" />
<action type="Redirect" url="/Products" />
</rule>
</rules>
</rewrite>
</system.webServer>
This was generated in IIS. I have tried to set the stopProcess attribute to true... which is a solution that I've seen in a few threads, but nothing is working.
This website is one of our legacy sites that uses an ISAPI module for Rewrites. Since we aren't using an Azure VM, we cannot continue using this method to rewrite.
Is there something I'm missing here?
Azure Web Sites does not have the URL Rewrite module installed, so the config approach won't work. Maybe you could update the site code to use ASP.NET URL routing, as described in this blog post by Scott Gu: http://weblogs.asp.net/scottgu/archive/2009/10/13/url-routing-with-asp-net-4-web-forms-vs-2010-and-net-4-0-series.aspx
Azure Web Sites didn't used to have the URL Rewrite Module installed, but now it does, so you can just use the standard <system.webServer><rewrite> section in your web.config.

Url rewrite in ASP.NET MVC or web.config

Ignore the base problem. My custom built javascript library is moronic when it comes to looking up resources. Ok, it's me, but I don't care. At this point I'm looking for an ugly work around.
The client requests
http://localhost/Activities/Index/dojo/resources/blank.gif
and I want to say, your're a moron behind my teeth, but front with a smile and serve the image which of course is located at
http://localhost/dist/dojo/resources/blank.gif
No redirect. I guess it's what's called a rewrite. Solve that for me, please. Either in the route table or web.config, whichever performs the best.
This could be easily achieved with the IIS 7.0 rewrite module:
<rewrite>
<rules>
<rule name="resources">
<match url="Activities/Index/(.*)" />
<action type="Rewrite" url="dist/{R:1}" />
</rule>
</rules>
</rewrite>

Convert ASP.NET Webforms url to MVC route

I'm replacing an old ASP.NET webforms app with a new MVC application. However, I have an issue with users having old links to a particular page that I would like to automatically translate to the correct MVC route.
Old site: http://mysite.com/ticketsdetail.aspx?id=12345
New site: http://mysite.com/tickets/details/12345
Is there a way in the MVC routing of catching the old url and translating it to the new one?
EDIT:
Ok, the web.config entry to do this using the IIS7 URL rewriting is:
<rewrite>
<rules>
<rule name="Ticket page redirect" stopProcessing="true">
<match url="ticketsdetail.aspx$" />
<conditions>
<add input="{QUERY_STRING}" pattern="id=(\d*)$" />
</conditions>
<action type="Redirect" url="Calls/Tickets/{C:1}" appendQueryString="false" redirectType="Temporary" />
</rule>
</rules>
</rewrite>
Don't do it in code is my suggestion, unless you absolutely need to. Scott Hanselman has a good article on how to do what you need in the web.conf using IIS Url Rewrite.
Article Here
This would be your rule that would go in your web.config as well:
<rule name="RewriteUserFriendlyURL1" stopProcessing="true">
<match url="^ticket/details/([^/]+)/?$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="ticketdetails.aspx?id={R:1}" />
</rule>
If your using IIS7 then the URL rewrite tool is pretty slick;
http://learn.iis.net/page.aspx/460/using-url-rewrite-module/
This is a good article;
http://learn.iis.net/page.aspx/496/iis-url-rewriting-and-aspnet-routing/
Which Option Should You Use?
What does all this information mean if
you need to choose a technology to
enable clean URLs for your Web
applications? In this section, we
explain how to make this choice.
If your Web application is built by
using anything except ASP.NET, use the
IIS URL Rewrite module. Otherwise, the
rules are:
If you are developing a new ASP.NET Web application that uses
either ASP.NET MVC or ASP.NET Dynamic
Data technologies, use ASP.NET
routing. Your application will benefit
from native support for clean URLs,
including generation of clean URLs for
the links in your Web pages. Note that
ASP.NET routing does not support
standard Web Forms applications yet,
although there are plans to support it
in the future.
If you already have a legacy ASP.NET Web application and do not
want to change it, use the URL Rewrite
module. The URL Rewrite module lets
you translate search engine-friendly
URLs into a format that your
application currently uses. Also, it
lets you create redirect rules that
can be used to redirect search engine
crawlers to clean URLs.
However, its logical to me to get the url rewriter to sort out this kind of stuff out at the iis level, especially since you'll be needing to use a regex to pull the querystring apart to form the new url.. Something the url rewriter does amazingly well.

How to host an asp.net mvc app on a domain that points to a subfolder?

I have the folowing scenario:
www.somedomain.com -> this points to a folder on a shared host, say /MyFolder1
www.otherdomain.com -> this points to another folder on the same shared host, say /MyFolder2
With asp.net mvc my urls get mapped to:
www.somedomain.com/MyFolder1/Action
www.somedomain.com/MyFolder2/Action
I (obviosly) dont't want to have "MyFolder1" and "MyFolder2" on my URLs. How do i solve this on asp.net MVC?
I want to have:
www.somedomain.com/Action
www.somedomain.com/Action
But i need to keep the subfolders on IIS (or some other solution that allows me to have two sites, with different domains on the same hosting).
Help is very much appreciated.
Thanks
The best way to handle this IMO is to use rewriting at the IIS level. I just did this on a site using IIS 7 URL Rewrite. If you don't have this module installed on your host provider, you can try to use one of the other URL rewriting tools. But, for example on DiscountASP you can use IIS 7 URL rewrite.
First you need to point all your domains to your current site. Then when you download the tool: http://blogs.iis.net/bills/archive/2008/05/31/urlrewrite-module-for-iis7.aspx, it provides a GUI for editing the rules. Ultimately the rules are placed into your web.config file. You want your rules to look something like this:
<rewrite>
<rewriteMaps>
<rewriteMap name="otherdomain" />
</rewriteMaps>
<rules>
<rule name="otherdomain" stopProcessing="false">
<match url=".*" />
<conditions>
<add input="{HTTP_HOST}" pattern="otherdomain.com" />
</conditions>
<action type="Rewrite" url="/site_folder/otherdomain/{R:0}" />
</rule>
</rules>
</rewrite>
If you are using ISAPI rewrite, I'll probably have that one soon as well for another host that I'm using that doesn't support IIS rewrite
My hosting provider is using IIS 6 with ISAPI_Rewrite.
It works fine, www.somedomain.com/MyFolder1/Action becomes www.somedomain.com/Action as i want on the entry page. But all Url.ActionLink() maps again to www.somedomain.com/MyFolder1/Action which i don't want.
The easiest thing to do would be to create a subdomain on the shared host. Something like app1.somedomain.com and app2.somedomain.com, the subdomain maps directly to the subdirectory where the application is and that becomes the root.

Resources