Sorted By: Month (2) and Year (2011)

Running ASP.NET MVC Under IIS 6.0 and IIS 7.0 Classic Mode : Solution to Routing Problem

In this blog post, we will see how to run ASP.NET MVC application under IIS 6.0 and IIS 7.0 classic mode with some configurations on IIS and Global.asax file...
2011-02-26 07:31
Tugberk Ugurlu


Highway sign

I wrote a blog post on Deployment of ASP.Net MVC 3 RC 2 Application on a Shared Hosting Environment Without Begging The Hosting Company couple of months ago. The solution was working for most case scenarios if the server is configured properly for ASP.NET Routing. Other working case I have seen was the applications which are running under IIS 7.0 integrated mode. Under IIS 7.0 integrated mode, no special configuration necessary to use ASP.NET Routing.

As we know, one of the most beautiful parts of ASP.NET MVC framework is Routing. We have nice, clean, extensionless URLs thanks to routing and this is becoming an issue under IIS 6.0 and IIS 7.0 classic mode.

When we typing the path of a web site page inside the address bar of our web browser, we are making a request against server. If our web application is running under any version of IIS, the request hits ASP.NET framework on certain conditions. Especially, Older versions of IIS only map certain requests to the ASP.NET framework. If the extension of the web request is aspx, ashx, axd or any other extensions which is specific for ASP.NET framework are being mapped to ASP.NET framework. So, in a MVC application the requests are not being mapped to ASP.NET framework. 

 image
(You will be getting this 404 exception when you hit the extensionless URL of your application)

No, do not throw away your precious, new born ASP.NET MVC application which you have created working along with Nuget PMC (which is perfect), EFCodeFirst and any other cool newbie stuff.  There are optional solutions for this problem and they are not like hard things to implement.

Although, if you have a Windows Server 2003 and thinking about going up to IIS 7.0 integrated or classic mode, stop right there my friend ! Because, IIS 7.0 is not compatible with Windows Server 2003. So you are stuck with IIS 6.0 for now and keep reading for the solution Smile

The solutions are optional as indicated. They depend on what kind of powers you have over your server. Here is the list of solution you might be interested;

Option 1 : I am the guy with the full control power over my server and I want to keep extensionless URLs

If you have full access over your server, you could create so called Wildcard Script Map so that you can use the default ASP.NET MVC route table with IIS 7.0 (in classic mode) or IIS 6.0. This Wildcard Script Map will map all requests to the web server to the ASP.NET framework.

I have no experience with this option, though. I had a problem like this within this week, I have solved it with the following option and didn’t want to use this one even if I have full control over my server. I am not a server pro, so I won’t be making any comments on how this will effect the requests flow made against your server. I just thought that “man, every single request which made against my server will be mapped to ASP.NET framework. This could effect the speed of the delivery process.” and that how I skipped this option.

And, bad news guys Sarcastic smile Microsoft, also, indicated the following line of sentence one of their web site;

“Be aware that this option causes IIS to intercept every request made against the web server. This includes requests for images, classic ASP pages, and HTML pages. Therefore, enabling a wildcard script map to ASP.NET does have performance implications.”

To implement this feature, you need to follow some steps and here is the text I grabbed from http://asp.net/ ;

 

image_thumb[5]

Here's how you enable a wildcard script map for IIS 7.0 (classic mode):

  1. Select your application in the Connections window
  2. Make sure that the Features view is selected
  3. Double-click the Handler Mappings button
  4. Click the Add Wildcard Script Map link
  5. Enter the path to the aspnet_isapi.dll file (You can copy this path from the PageHandlerFactory script map)
  6. Enter the name MVC Click the OK button

 

 

 

image_thumb[4]

Follow these steps to create a wildcard script map with IIS 6.0:

  1. Right-click a website and select Properties
  2. Select the Home Directory tab
  3. Click the Configuration button
  4. Select the Mappings tab
  5. Click the Insert button
  6. Paste the path to the aspnet_isapi.dll into the Executable field (you can copy this path from the script map for .aspx files)
  7. Uncheck the checkbox labeled Verify that file exists
  8. Click the OK button

 

Option 2 : I am the guy who has the full control power over my server, cares about performance and not care about URLs

This option is the one of the other option I do not like very much but maybe you will Smile So here is the deal;

We will simply add Extensions to the Route Table so that older versions of IIS can pass requests to the ASP.NET framework. This option requires changes inside Global.asax file of you application and some addition work on IIS for modifying the Default route so that it includes a file extension that is mapped to the ASP.NET framework.

Let’s see the RegisterRoutes method inside your Global.asax file;

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

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

As you see here on line 7, we are aiming to get URLs without extension. Let’s see what it needs to be look like after we change it in order to implement this option;

        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 = "" }
            );


        }

Here, we are assigning .mvc extension for every URL for controller name.

I want to warn you about your links inside your views. If you created them by hard coding, now you are officially screwed my friend. Because, you either have to change all of them by hand or need to implement option 1 in order to keep them unbreakable. If you used ActionLink or RouteLink kind of way, then your are good to go. Changes will be handled by MVC Framework for you.

Therefore, to get ASP.NET Routing to work, we must modify the Default route so that it includes a file extension that is mapped to the ASP.NET framework.

This is done using a script named registermvc.wsf. It was included with the ASP.NET MVC 1 release in C:\Program Files\Microsoft ASP.NET\ASP.NET MVC 1.0\Scripts, but as of ASP.NET 2 this script has been moved to the ASP.NET Futures, available at http://aspnet.codeplex.com/releases/view/39978.

Executing this script registers a new .mvc extension with IIS. After you register the .mvc extension, you can modify your routes in the Global.asax file so that the routes use the .mvc extension.

After this implementation, your URLs will look like this;

/Home.mvc/Index/

/Product.mvc/Details/3

/Product.mvc/

Option 3 : I am the guy who has no control power over my server, not care about URLs (If you care, it does not matter. You have no choice)

This option is the easiest way of make your application up and running within minutes depending on your application structure. Only you need to do here is; making some changes inside your Global.asax file, recompiling your application and publishing it into your server. That’s all. Let’s see how our new RegisterRoutes method inside Global.asax file needs to be look like;

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

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

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


        }

Look at line 7. Isn’t it familiar? It is one of the ASP.NET framework extensions. You could add .aspx or whatever you want from ASP.NET framework extensions. It is already registered into the Default route so the requests will be mapped to the ASP.NET framework.

I hope that this is the solution you are looking for your problem.

ASP.NET MVC : Throwing 404 Exceptions Manually From Controller When Model is Null

This post is a quick demonstration of how you can throw HttpException of 404 manually from a controller on ASP.NET MVC when the model you're passing is null
2011-02-17 09:44
Tugberk Ugurlu


How do you handle your actions inside a controller when the model you are passing to the view is null? Sometimes it is best to show ‘There is no product as you requested’ kind of message but sometime it is getting dull. Especially on CRUD based actions.

HttpException is becoming very handy here. We could throw this exception from our controllers and the application will render the status exception page we are defining. In this post I would like to show how we handle this properly. Here is our scenario : we have an MVC app and in one of our edit page, we would like to throw 404 exception if the model we are passing is null.

 

        [Authorize]
        public ActionResult Edit(Guid id) {

            var model = Repository.GetSingle(id);

            if (model == null)
                throw new HttpException(404, "not found");

            return View(model);
        }

 

This code is basically telling that try to get the item whose ID is id and check if it is null. If it is null, throw HttpException whose status code is 404. If not, return the View along with passing model into view page.

When we hit a page which has null model, we will see the 404 page as expected;

image

This is that easy ! Give it a try, it won’t hurt Smile

Microsoft MIX11 : Students & Academic Staff Discount is Available For the Conference

Microsoft MIX11 : Students & Academic Staff Discount is Available For the Conference / The chance that every geek student wants to catch!
2011-02-16 06:22
Tugberk Ugurlu


default_center_keynoteNearly all of .Net developers are aware of MIX Talks (my guess, only developers who has no internet access are not aware of MIX. If you are one of those, stop writing code and go write child novels) and they are thrown every year by Microsoft. MIX11 is just a few months ahead of us and this year, it will take place in Mandalay Bay, Las Vegas between 12th of April and 14th of April. For those who has no idea what I am talking about, here is a brief overview of MIX which was quoted from http://live.visitmix.com/About

MIX is a unique opportunity to engage with Microsoft and industry professionals in a two-way conversation about the future of web - from the diversity of devices and interaction models on the front-end, to the tools and technologies that power the user experience, to the services that make it all possible.

MIX is for professionals who design and build cutting-edge websites. http://live.visitmix.com

PREVIEW THE NEXT WEB

Learn about the future of web, from the diversity of devices and interaction models on the front-end, to the tools and technologies that power the user experience, to the services that make it all possible. There’s no better place to hear about the future of Silverlight, Internet Explorer, Windows Phone, ASP.NET, and technologies like HTML5 and CSS3.

GET REAL ANSWERS FROM THE EXPERTS

MIX isn’t just about getting a first look at the latest technologies and trends – it’s an opportunity for you to have your questions answered by industry and Microsoft experts.

I thought that there could be an easy way for students to live those sessions where they happen. So I have searched (this is a Microsoft event so I thought that would be only legitimate if I Google it with Bing Smile) and nothing has come up and decided to try to get information by e-mail.

MIX registration team replied me at the same they with tons of information and I didn’t know that MIX has this kind of great deals for students.

Before diving deep, I would like to indicate that I have asked MIX registration team for the permission to publish the content of e-mail they have sent me on my blog so that the others can be aware of this opportunity and they kindly accept my request.

My question was so straight and their answer was so informative;

Q : I am a student on Travel Management, in Turkey and I would like to know if you have any discounts for students on MIX11 or not.

A : The academic rate is for full-time students and full-time staff of the academic community at the time of MIX11 with proof of enrollment or employment from an accredited educational institution. The cost to attend with a code is $595 and covers the regular conference fee only; Pre-Conference Boot Camps and hotel accommodations are additional.

Given the original cost which is $1395, this deal is pretty perfect. Of course you need to prove that you are eligible for the discount. How so? They informed me before I asked! Pretty gorgeous. The process is as indicated below;

Students

College, university, and career state-accredited school students qualify to register for MIX11 using the academic discounted rate with an acceptable form of academic ID.  Please provide one of the following to prove that you are a current, full-time student:

  • Official dated, current class schedule indicating name of school and student
  • Official dated, current school tuition bill indicating name of school and student (please black-out tuition cost and any other personal information)
  • Official dated, current report card indicating name of school and student
  • Other official dated, current proof of enrollment indicating name of school and student

Faculty / Staff

College, university, and career state-accredited faculty and staff members qualify to register for MIX11 using the academic discounted rate with an acceptable form of academic ID.  Please provide one of the following:

  • Official, current faculty/staff paycheck stub indicating full-time employment (please black-out salary information and any other personal information)
  • Official, current letter on school letterhead verifying person’s full-time employment

If you are an eligible academic and would like to attend MIX11, you need to submit your form of academic ID in one of the following ways:

  • Fax a photocopy of academic ID to 1-206-783-5594
    --OR--
  • Email a scanned image of academic ID to MIX11@ustechs.com in JPG, PDF or GIF format

After you complete those above steps, the process is going to be like below according to MIX Registration team;

Once we receive your valid proof of academic ID, we will provide you with an RSVP code to register.  Please note you will need to bring the original form of academic ID in addition to a current valid photo ID with you to the event and show it at the time of registration.  Failure to provide valid proof of eligibility is the responsibility of the registrant, and the registrant is responsible for their hotel and travel plans.

MIX11_BB_SeeYouAt_1Personally, I am not going to make it for this year due to the academic calendar but maybe you are. I would say that is a perfect deal to catch for students and this kind of events are the best way to integrate with Microsoft developers.

Pack your stuff, and get ready for those geek sessions Smile

Owww, I almost forgot. If you’ll be there for MIX, don’t forget to put the picture (just on the left side) on your blog to inform others that you will be there Smile

This is cool way to say “Hey, I am a geek and I am proud of it…”

Tags