thinkmmk
thinkmmk

Reputation: 487

Url routing problem with IIS7

Url routing and IIS7 03-24-2011 04:24 PM

Hi,

I have a query regarding Url Routing in asp.net 4.0 and IIS7. Below I have explained what is my website structure and how it is configured in IIS7.

Directory Structure:

1.MyWebsite>Index.aspx

2.MyWebsite>AboutUs>Index.aspx

3.MyWebsite>ContactUs>ContactUs.aspx

II7 Configuration:

I have configured my application's default document as Index.aspx Now, when i access below urls, IIS7 does few routing automatically (note that until now i havent added any routes in global.asax )

1.localhost/MyWebsite will be forwarded to localhost/MyWebsite/Index.aspx Ok !!

2.localhost/MyWebsite/Aboutus will be forwarded to localhost/AboutUs/Index.aspx (this redirection is done as parent level default document is inherited by AboutUs folder) Ok !!

3.localhost/MyWebsite/ContactUs == Http Error 403.14 Forbidden (This error is thrown by IIS7 becoz it doesnt find Index.aspx in Conactus folder) Fine !!

Ok so for localhost/MyWebsite/ContactUs to be routed to localhost/MyWebsite/ContactUs/Index.aspx I added below code in RegisterRoutes() method in global.asax hoping this will fulfill my requirement.

routes.MapPageRoute('ConactUs','ContactUs','~/ContactUs/ContactUs.aspx');

So now when i acces localhost/MyWebsite/ContactUs i still get the same Forbidden error. So is IIS7 default page setting at root not allowing my routes to work?

If I have Index.aspx page in each folder then it works i.e.redirection to /Contactus/Index.aspx ... but what if i dont have Index.aspx (my default document) in every page?

Thanks & Kind regards, m .

Upvotes: 2

Views: 787

Answers (3)

thinkmmk
thinkmmk

Reputation: 487

Ok here is what i did to work things for me. First of all let me again add few more details about my project..

Directory Structure:

1.MyWebsite>Index.aspx

2.MyWebsite>AboutUs>Index.aspx

3.MyWebsite>ContactUs>ContactUs.aspx

All my pages have a UserControl named logo.ascx. logo.ascx doesnt have a code behind file attached to it. It just have a html with resolve.clienturl() method. And logo.ascx just have below attribute; <%@ Control Language="C#" %>

So to make every thing working i did a couple of things,

  1. For every directory i selected different Default Document as per my need. so whenever a directory is browsed i will automatically be routed to my selected default page.
  2. I added route in global.asax files for non directory browsing.
  3. I made RouteExistingFiles= true;
  4. Then I made sure that whatever route i added , i replaced all the direct links to that page to short url. like I replaced localhost/MyWebsite/ContactUs/ContactUs.aspx with localhost/MyWebsite/ContactUs throughout my project (like in menus, sub menus etc.).
  5. But doing all this had a weird problem, i wasnt able to see my logo image. I was sure that i had used resolve.cienturl() and it was working fine in all my non routing pages. But only for pages that had their route added image wasnt visible. To make this working all i had to do was just add a code behind file to my logo.ascx and magic it worked.

Thanks naraen thanks pauli for ur support and suggestions.

Upvotes: 1

Pauli &#216;ster&#248;
Pauli &#216;ster&#248;

Reputation: 6916

Have you looked at the RouteExistingFiles property of RouteCollection. This will allow you to specify whether routing should be used for existing files and folders or not. Default value is false, which means that if there is a clash between a route and a file or folder already, the route will be ignored.

Upvotes: 0

Naraen
Naraen

Reputation: 3250

The reason it isn't working is because the IIS handler is finding that the folder "ContactUs" exists and is responding to the request instead of handing off to the ASP.NET handler. (For e.g. if your actual physical folder was called 'ContactUs1' instead of 'ContactUs' and updated your route to point to '~/contactus1/contactus.aspx' you'd see your routing work).

The simplest solution in your case might be to set 'ContactUs.aspx' as the default document just for the 'ContactUs' subfolder. You can do this by adding a web.config to the ContactUs folder with the following XML section below.

<configuration>
    <system.webServer>
        <defaultDocument>
            <files>
                <add value="ContactUs.aspx" />
            </files>
        </defaultDocument>
    </system.webServer>
</configuration>

Upvotes: 1

Related Questions