views:

598

answers:

4

I'm using Visual Studios' built-in ASP.NET Development Server (VWD) to test my web site during development.

I would like this ASP.NET web site to use extensionless URLs (pages don't require the aspx extension). Ordinarily I would configure a custom 404 in IIS that directs to an ASPX page. How would I do this with VWD?

P.S. This is not an ASP.NET MVC web site.

+1  A: 

There's nothing special you need to do. Just remove the .aspx extension from the ASPX page file and it should work fine with VWD. The Visual Studio designer will probably complain that there's no build provider registered for the extension '', but you can just ignore it. Then you can reference the page without extension:

http://localhost:2181/Default

<%@ Page Language="C#"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    Hello World
    </div>
    </form>
</body>
</html>
Darin Dimitrov
+1  A: 

If you are trying to get something like http://localhost:3000/value to go to http://localhost:3000/page.aspx?tag=value then you can use an HttpModule, which is also a good alternative to a 404 redirect. I used to do the same thing too.

I posted some example code in a previous question.

DavGarcia
+1  A: 

Here is an example of a Web.Config using UrlRewritingNet. Doing this will allow you to do extensionless Rewriting without having to write any HttpModule or anything like that.

(full article here)

Note: this requires IIS7 as it is using the system.webServer section of the web.config.


<configSections>  
    <section name="urlrewritingnet"    
             restartOnExternalChanges="true"    
             requirePermission="false"    
             type="UrlRewritingNet.Configuration.UrlRewriteSection,  UrlRewritingNet.UrlRewriter" />  
</configSections>  

<system.webServer>  
    <modules runAllManagedModulesForAllRequests="true">   
        <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />  
    </modules>  
</system.webServer>  


<urlrewritingnet rewriteOnlyVirtualUrls="true"    
                 contextItemsPrefix="QueryString"    
                 xmlns="http://www.urlrewriting.net/schemas/config/2006/07"&gt;   
    <rewrites>  
        <!--Enable HTM(L) Extensions-->  
        <add name="pageHTML"    
             virtualUrl="^~/(.+).htm(.*)"    
             redirectMode="Permanent"  
             rewriteUrlParameter="ExcludeFromClientQueryString"    
             destinationUrl="~/$1.aspx"    
             ignoreCase="true" />  
        <!--Fix the WebResource JS Error-->  
        <add name="WebResourceFix"    
             virtualUrl="^~/WebResource.axd(.*)"  
             rewriteUrlParameter="IncludeQueryStringForRewrite"    
             destinationUrl="~/WebResource.axd$1"    
             ignoreCase="true"/>   
        <!--Fix the ScriptResource JS Error-->  
        <add name="ScriptResource"    
             virtualUrl="^~/ScriptResource.axd(.*)"  
             rewriteUrlParameter="IncludeQueryStringForRewrite"    
             destinationUrl="~/ScriptResource.axd$1"    
             ignoreCase="true"/>   
        <!--Allow Extensionless Page Extensions-->  
        <add name="pageExtensionless"  
             virtualUrl="^~/(.+)$"  
             redirectMode="Permanent"  
             rewriteUrlParameter="ExcludeFromClientQueryString"  
             destinationUrl="~/$1.aspx"  
             ignoreCase="true" />  
    </rewrites>  
</urlrewritingnet>

rockinthesixstring
+1  A: 

All you need to do is add the module in two different places within your web.config...

<system.web>
    <pages theme="Default" />
    <httpModules>
        <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
    </httpModules>
</system.web>


<system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
        <remove name="UrlRewriteModule"/>
        <add name="UrlRewriteModule" type="UrlRewritingNet.Web.UrlRewriteModule, UrlRewritingNet.UrlRewriter" />
    </modules>
</system.webServer>

The first one is to add it to your httpModules with will work in your VS Dev environment, and the second one will for IIS7

rockinthesixstring