带和不带.aspx扩展名的链接

时间:2008-09-28 19:47:46

标签: c# asp.net vb.net url-rewriting iis-6

是否可以配置服务器以允许使用和不使用.aspx扩展的链接。

如果是,我该如何设置呢。

我正在使用umbraco的客户端网站上工作。我知道它内置了友好的URL功能。不幸的是,该网站已经存在,并且为所有链接启用了该功能。

问题是他们想要使用像www.sitename.com/promotion这样的促销网址,而不必附加.aspx扩展名。而且我们不希望在网站上重新启用网址并且必须跟踪所有损坏的链接时遇到麻烦。

3 个答案:

答案 0 :(得分:3)

答案 1 :(得分:1)

我之前通过编写一个简单的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.
        }
    }

答案 2 :(得分:0)

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