具有和不具有.aspx扩展名的链接

时间:2020-03-06 14:51:04  来源:igfitidea点击:

可以将服务器配置为允许使用带有或者不带有.aspx扩展名的链接。

如果是,我该如何进行设置。

我正在使用umbraco的客户端站点上工作。我知道它具有友好的URL功能。不幸的是,该站点已经启用,并且可以打开所有链接的功能。

问题在于他们希望使用促销网址,例如www.sitename.com/promotion,而不必添加.aspx扩展名。而且,我们不想麻烦地在整个站点范围内启用url重写并必须跟踪所有损坏的链接。

解决方案

斯科特·格思里对此有很好的发表。

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

在"完全控制URI"部分下的一半位置提供了链接和许多实现此目的的方法:
http://blogs.msdn.com/bags/archive/2008/08/22/rest-in-wcf-part-ix-controlling-the-uri.aspx

我之前通过编写一个简单的HttpModule来完成此操作,需要注意以下几点:

  • 我们需要将IIS中的404错误指向一个aspx页面,否则IIS将不会调用ASP.NET运行时,并且HTTPModule将永远不会出现。
  • 这最适合捕获虚构网址并从虚假网址重定向,而不是作为功能齐全的urlrewrite。
public class UrlRewrite : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.BeginRequest += (new EventHandler(this.Application_BeginRequest));
        }

        private void Application_BeginRequest(Object source, EventArgs e)
        {
            // The RawUrl will look like:
            // http://domain.com/404.aspx;http://domain.com/Posts/SomePost/
            if (HttpContext.Current.Request.RawUrl.Contains(";")
                && HttpContext.Current.Request.RawUrl.Contains("404.aspx"))
            {
                // This places the originally entered URL into url[1]
                string[] url = HttpContext.Current.Request.RawUrl.ToString().Split(';');

                // Now parse the URL and redirect to where you want to go, 
                // you can use a XML file to store mappings between short urls and real ourls.

                string newUrl = parseYourUrl(url[1]);
                Response.Redirect(newUrl);
            }

            // If we get here, then the actual contents of 404.aspx will get loaded.
        }

        public void Dispose()
        {
            // Needed for implementing the interface IHttpModule.
        }
    }