如何使用 Visual Web Development 服务器配置无扩展名 URL?

发布于 2024-07-11 19:05:03 字数 222 浏览 6 评论 0原文

我正在使用 Visual Studios 的内置 ASP.NET 开发服务器 (VWD) 在开发过程中测试我的网站。

我希望这个 ASP.NET 网站使用无扩展名 URL(页面不需要 aspx 扩展名)。 通常我会在 IIS 中配置一个自定义 404 页面,指向 ASPX 页面。 我该如何使用 VWD 来做到这一点?

PS 这不是 ASP.NET MVC 网站。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

来日方长 2024-07-18 19:05:03

以下是使用 UrlRewritingNet 的 Web.Config 示例。 这样做将允许您进行无扩展重写,而无需编写任何 HttpModule 或类似的内容。

(完整文章 此处)

注意:这需要 IIS7,因为它使用 web.config 的 system.webServer 部分。

<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">   
    <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>  

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">   
    <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>  

风启觞 2024-07-18 19:05:03

您无需做任何特别的事情。 只需从 ASPX 页面文件中删除 .aspx 扩展名,它就可以与 VWD 一起正常工作。 Visual Studio 设计者可能会抱怨没有为扩展“”注册构建提供程序,但您可以忽略它。 然后你就可以引用没有扩展名的页面:

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">
<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>

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">
<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>
高跟鞋的旋律 2024-07-18 19:05:03

如果您尝试获取类似 http://localhost:3000/value 的内容,请转到 http://localhost:3000/page.aspx?tag=value 那么你可以使用 HttpModule,这也是 404 重定向的一个很好的替代方案。 我也曾经做过同样的事情。

我在上一个问题中发布了一些示例代码

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.

心作怪 2024-07-18 19:05:03

您需要做的就是在 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>

第一个是将其添加到您的 httpModules 中,它将在您的 VS Dev 环境中工作,第二个将用于 IIS7

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文