【问题标题】:URL Rewrite ASP.netURL 重写 ASP.net
【发布时间】:2012-10-29 21:22:27
【问题描述】:

我有一个 asp.net 网站,我需要在其中使用 URL 重写,所以我编写了一个 HTTP 模块并且我已经实现了它并且它可以正常工作,唯一的问题是当页面重定向到其相应地址时,图像和样式未加载。

这里是http模块:

// 你的 BeginRequest 事件处理程序。

private void Application_BeginRequest(Object source, EventArgs e)
{
    HttpApplication application = (HttpApplication)source;
    string URL = application.Request.Url.ToString();
    //int pid = Convert.ToInt32(application.Request.QueryString["pid"]);

    if ((URL.ToLower().Contains(".aspx"))
        || (URL.ToLower().Contains(".js"))
        || (URL.ToLower().Contains(".css"))
        || (URL.ToLower().Contains(".gif"))
        || (URL.ToLower().Contains(".png"))
        || (URL.ToLower().Contains(".jpeg"))
        || (URL.ToLower().Contains(".jpe"))
        || (URL.ToLower().Contains(".jpg"))
        || (URL.ToLower().Contains(".ashx")))
        return;
    else
    {
        string mname = URL.Substring(URL.LastIndexOf("/") + 1).ToString();

        Merchand ms = merchantDB.GetMerchant(mname);

        HttpContext context = application.Context;
        if (ms != null)
        {

            string url = "~/pages/Merchant.aspx?mid=" + ms.MerchandID + "&catid=" + ms.MainCategory + "&subcatid=0";
            context.RewritePath(VirtualPathUtility.ToAppRelative(url));
        }
        else
        {
            //("");
            string url = "~/pages/default.aspx";
            context.RewritePath(VirtualPathUtility.ToAppRelative(url));
        }
    }

}

当我从正常的 URL 打开页面时,它可以正常打开,但是当我使用 URL 时,它会重写打开但没有图像或样式。

当我打开 firebug 时,我收到一个错误,即找不到 css 和 javascript

【问题讨论】:

    标签: c# asp.net url-rewriting


    【解决方案1】:

    要使用 IIS 进行重写,请执行以下操作:

    1. 在system.webserver标签的web.config中注册httpmodule:
    <modules>
    <add name="Rewrite" type="Rewrite"/>
    </modules>
    
    1. 改变这个:context.RewritePath(VirtualPathUtility.ToAppRelative(url));为此: context.RewritePath(url, false);
    2. 让您的图像在服务器上运行并将它们的路径设置为 ~/images/imagename

    【讨论】:

      猜你喜欢
      • 2011-09-18
      • 2010-09-05
      • 1970-01-01
      • 1970-01-01
      • 2015-02-15
      • 2017-09-10
      • 2011-11-27
      • 1970-01-01
      相关资源
      最近更新 更多