删除HTML或ASPX扩展

roa*_*own 35 html asp.net iis-7 url-rewriting

在托管的IIS7环境中,我正在寻找使用无扩展名文件名的最简单方法.我只有以下页面:

index.html(或.aspx) - > domain.com gallery.html - > domain.com/gallery videos.html - > domain.com/videos等...

我只有一些页面,我没有动态代码,没什么特别的.我开发我发现所有的例子或我在其他网站使用方法围绕动态内容,网页等,我只是在寻找最简单的解决方案,非常不需要安装任何形式的URL重写模块.最好是,我可以保留.html扩展名,而不是将站点转换为ASP.NET项目,但这是一个选项.

谢谢.

roa*_*own 48

我最终使用以下网站:

http://blogs.msdn.com/b/carlosag/archive/2008/09/02/iis7urlrewriteseo.aspx

http://forums.iis.net/t/1162450.aspx

或者基本上我的web.config文件中的以下代码使用大多数托管站点现在提供的IIS7 URL重写模块(在这种情况下我使用的是GoDaddy):

<system.webServer>
    <rewrite>
        <rules>
            <rule name="RewriteASPX">
                <match url="(.*)" />
                <conditions logicalGrouping="MatchAll">
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="{R:1}.aspx" />
            </rule>
        </rules>
    </rewrite>
</system.webServer>
Run Code Online (Sandbox Code Playgroud)

  • 感谢这个出色的解决方案.为了澄清,上面的XML在web.config的<system.webServer>元素中. (13认同)

Rig*_*iga 8

另一种更现代的方法是使用Microsoft.AspNet.FriendlyUrls.在Global.asax.cs中添加:

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    RouteConfig.RegisterRoutes(RouteTable.Routes);
Run Code Online (Sandbox Code Playgroud)

并在RouteConfig文件中

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
Run Code Online (Sandbox Code Playgroud)


Paw*_*ari 6

实现同样的另一个最简单的解决方案:

将以下代码行放入global.ascx文件中:

void Application_BeginRequest(object sender, EventArgs e)
{
   String fullOrigionalpath = Request.Url.ToString();
   String[] sElements = fullOrigionalpath.Split('/');
   String[] sFilePath = sElements[sElements.Length - 1].Split('.');

   if (!fullOrigionalpath.Contains(".aspx") && sFilePath.Length == 1)
   {
       if (!string.IsNullOrEmpty(sFilePath[0].Trim()))
           Context.RewritePath(sFilePath[0] + ".aspx");
   }
}
Run Code Online (Sandbox Code Playgroud)

  • 此代码允许页面在没有.aspx的情况下工作,但它不会删除.aspx (3认同)