Deploying MVC Application to IIS 6.0 - Routes

774
Views
1
Answers

Hi,
I am having troubles in deploying MVC application to IIS 6.0. When I create a new website and deploy the application everything works fine, But when I create a virtual application I cant browse the appliction.

Is there something to do with routes in global.asax? or with the settings in IIS?

edit | flag
William Patrick
Asked on: Jan 17, 2012 at 8:39PM

1 Answers

100
10
100

I guess your application works fine if its deployed in a new website and calling it by
http:/localhost:8080/
but not working when you create a virtual directory under 8080 and call it by
http:/localhost:8080/VD/

Reason:ASP.NET project is scoped to the Website or Virtual Application in which it resides
Follow the steps to solve the issue.

Configuring IIS

  1. Right clicking on the Virtual Application node (VD in this case) and select Properties
  2. Go to the Virtual Directory tab and select Configuration
  3. You will see Application Configuration dialog which displays a list of ISAPI mappings. Scroll down to see if .mvc is in the list.
  4. If it is in the list on your machine, you can skip to Step 8. If it’s not in the list, let’s add it to the list.
  5. Click Add which will bring up an empty Add/Edit Application Extension Mapping dialog.
  6. Generally the path would be C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll. Check if this is correct in your case.
  7. Fill in the fields with the exact path and the extension should be .mvc .
  8. Make sure that you have specified verbs - Limit to:GET,POST,HEAD,DEBUG and Not ALL.
  9. Make sure you have unchecked "Verify that file exists" option.
  10. Click OK and you’re done with the mapping.

Configuring Routes with an Extension

For extension less url Just skip this step.
Lets update the default routes to look for the file extension we chose, whether it be .mvc or .aspx extension. Use the RegisterRoutes method in your Global.asax.cs file using the .mvc extension. If you want to use the .aspx extension, just replace {controller}.mvc with {controller}.aspx.

public static void RegisterRoutes(RouteCollection routes)
{
  routes
.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

Note that I have specified route named “Root” which can match requests for the application root. In this case it is
http:/localhost:8080/VD/Mypage.mvc

Possible Traps:
Your Css and Javascript files may become unavailable in this case. To over come this male sure you have used
=Url.Content("url")while calling css and scripts.
You can also specify

HttpContext.Current.RewritePath(Request.ApplicationPath, false);

in your code behind.Because in the virtual application, the URL to the stylesheet rendered contained the virtual application name, when it shouldn’t. Changing the second argument to false fixes this.

IIS6 Extension-less URLs

To make extension-less URLs work let us use “Star mapping” or “Wildcard mapping” feature of IIS 6.

Follow these steps

  1. Go back to the Application Configuration Properties dialog like we did when configuring the .mvc ISAPI mapping
  2. Scroll for .mvc extension
  3. Next to the Wildcard application maps section, click the Insert… button.
  4. This brings up the wildcard application mapping dialog. Enter the path(C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll) to the aspnet_isapi.dll.
  5. Don’t forget to uncheck the Verify that file exists checkbox!
  6. Go back and reset the routes in your Global.asax.cs file to the default routes. You no longer need the .mvc file extension in the routes as we are using extension less URL's

Your routes wil look like
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

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

Now when you browse your site, your URLs will not have a file extension

You can use the application like a normal website by calling **http:/localhost:8080/VD/**

edit | flag
Gokul A
Answered on: Jan 17, 2012 at 9:36PM

Post your Answer

Search

Welcome to Ask Amoeba!
This is 100% free and interactive site for sharing professional Questions and Answers, Opensource projects, Interview questions.
Learners, beginners, Experts stop and share your knowledge and ideas!

Browse Categories

Browse Tags