删除 HTML 或 ASPX 扩展

发布于 2024-10-08 21:38:38 字数 481 浏览 0 评论 0原文

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

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

我只有少数页面,没有动态代码,没有什么特别的。我发现的所有示例或我在开发的其他站点中使用的方法都围绕动态内容、页面等。我只是在寻找最简单的解决方案,理想情况下不需要安装任何类型的 URL 重写模块。我最好保留 .html 扩展名,而不是将网站转换为 ASP.NET 项目,但这是一个选项。

In a hosted IIS7 environment, I am looking for the simplest way to use extension-less file names. Simply I have the following pages:

  • index.html (or .aspx) --> example.com
  • gallery.html --> example.com/gallery
  • videos.html --> example.com/videos
  • etc...

I only have a handful of pages, I have no dynamic code, nothing special. All the examples I have found or methods I use in other sites I've developed revolve around dynamic content, pages, etc. I am simply looking for the simplest solution, ideally not requiring any sort of URL rewrite module installed. Preferably, I could keep the .html extension instead of converting the site to a ASP.NET project, but that is an option.

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

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

发布评论

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

评论(7

逆光下的微笑 2024-10-15 21:38:38

我最终使用了以下网站:

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>

I ended up using the following sites:

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

and

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

or basically the following code in my web.config file using the IIS7 URL Rewrite Module that most hosted sites now offer (in this case I am using 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>
记忆で 2024-10-15 21:38:38

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

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

并在 RouteConfig 文件中

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

Another little bit more modern way to do this is using Microsoft.AspNet.FriendlyUrls. In the Global.asax.cs add:

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

and in the RouteConfig file

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings);
    }
心在旅行 2024-10-15 21:38:38

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

将以下代码行放入您的 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");
   }
}

Another simplest solution for achieving the same:

Put following lines of code into your global.ascx file:

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");
   }
}
差↓一点笑了 2024-10-15 21:38:38

如果您有动态代码,我认为最简单的方法是将文件从 .aspx 重命名为 .html,特别是如果您只有少数页面。没有简单的方法可以在不以某种方式重写 URL 的情况下做到这一点。

然而,在 IIS 7 中,您可以使用 HTTP 模块非常轻松地进行设置。斯科特·格思里(Scott Guthrie)很好地解释了这一点。在这篇文章中,他展示了几种自定义 URL 的方法。我认为您最喜欢方法#3。

http:// /weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

If you have dynamic code, I think that the easiest thing to do is to just rename the files from .aspx to .html especially if you only have a handful of pages. There is no simple way to do it without rewriting the URL somehow.

However, with IIS 7, you can set it up really easily with an HTTP Module. Scott Guthrie explains this really well. In this post, he shows several approaches to customizing the URLs. I think that you would like approach #3 the best.

http://weblogs.asp.net/scottgu/archive/2007/02/26/tip-trick-url-rewriting-with-asp-net.aspx

用心笑 2024-10-15 21:38:38

我没有足够的观点来发表评论,这正在改进 Pawan M 的答案。除非您在页面上使用查询字符串,否则他会起作用。我已经修改了 Pawan 的代码以允许查询字符串,更不用说我的是 vb 版本。

检查并确保您的项目中有 Global.asax.vb 文件。如果没有通过执行以下操作添加项目:

文件 ->新->文件->全局应用程序类

在项目的 Global.asax 文件中添加以下函数:

Sub Application_BeginRequest(sender As Object, e As EventArgs)
    Dim fullOrigionalpath As [String] = Request.Url.ToString()
    Dim sElements As [String]() = fullOrigionalpath.Split("/"c)
    Dim sFilePath As [String]() = sElements(sElements.Length - 1).Split("."c)
    Dim queryString As [String]() = sElements(sElements.Length - 1).Split("?"c)

    If Not fullOrigionalpath.Contains(".aspx") AndAlso sFilePath.Length = 1 Then
        If Not String.IsNullOrEmpty(sFilePath(0).Trim()) Then
            If queryString.Length = 1 Then
                Context.RewritePath(sFilePath(0) + ".aspx")
            Else
                Context.RewritePath(queryString(0) + ".aspx?" + queryString(1))
            End If

        End If
    End If
End Sub

I don't have enough points to comment, and this is improving Pawan M's answer. His will work, unless you have Query Strings being used on the page. I have modified Pawan's code to allow for query strings, not to mention mine is the vb version.

Check to make sure your project has a Global.asax.vb file in it. If it doesn't Add an Item by doing this:

File -> New -> File -> Global Application Class

In the Global.asax file of your project add this function:

Sub Application_BeginRequest(sender As Object, e As EventArgs)
    Dim fullOrigionalpath As [String] = Request.Url.ToString()
    Dim sElements As [String]() = fullOrigionalpath.Split("/"c)
    Dim sFilePath As [String]() = sElements(sElements.Length - 1).Split("."c)
    Dim queryString As [String]() = sElements(sElements.Length - 1).Split("?"c)

    If Not fullOrigionalpath.Contains(".aspx") AndAlso sFilePath.Length = 1 Then
        If Not String.IsNullOrEmpty(sFilePath(0).Trim()) Then
            If queryString.Length = 1 Then
                Context.RewritePath(sFilePath(0) + ".aspx")
            Else
                Context.RewritePath(queryString(0) + ".aspx?" + queryString(1))
            End If

        End If
    End If
End Sub
羞稚 2024-10-15 21:38:38

您可以在 C# 中执行此操作,以便在 ASP.NET 中的 URL 中使用自定义扩展。

让代码中的“.recon”成为您的自定义扩展名。 (即将“.recon”替换为您自己的扩展名)

protected void Application_BeginRequest(object sender, EventArgs e)
 {
        HttpApplication app = sender as HttpApplication;
        if (app.Request.Path.ToLower().IndexOf(".recon") > 0)
        {
            string rawpath = app.Request.Path;
            string path = rawpath.Substring(0, rawpath.IndexOf(".recon"));
            app.Context.RewritePath(path+".aspx");
        }
 }

You can do this in c# to use a customized extension in your URL in ASP.NET.

Let ".recon" in the code be your customized extension. (i.e replace ".recon" to your own extension)

protected void Application_BeginRequest(object sender, EventArgs e)
 {
        HttpApplication app = sender as HttpApplication;
        if (app.Request.Path.ToLower().IndexOf(".recon") > 0)
        {
            string rawpath = app.Request.Path;
            string path = rawpath.Substring(0, rawpath.IndexOf(".recon"));
            app.Context.RewritePath(path+".aspx");
        }
 }
心安伴我暖 2024-10-15 21:38:38

删除特定页面的 asp WebForm 中的 .aspx 扩展名的简单解决方案:

检查解决方案资源管理器>>转到 Global.asax 文件>>检查方法Application_BeginRequest 在该方​​法中写入此代码

// Remove aspx Extension From Smy Page
string CurrentPath = Request.Path; // getting Current Url
if(CurrentPath == "/YourPageURL")
    HttpContext MyContext = HttpContext.Current;
    MyContext.RewritePath("/YourPageURL.aspx");

Easy Solution For Removing .aspx Extension in asp WebForm for Specific Page:

Check Solution Explorer >> Got to Global.asax File >> Check Method Application_BeginRequest Write this code inside this method

// Remove aspx Extension From Smy Page
string CurrentPath = Request.Path; // getting Current Url
if(CurrentPath == "/YourPageURL")
    HttpContext MyContext = HttpContext.Current;
    MyContext.RewritePath("/YourPageURL.aspx");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文