簡體   English   中英

URL重寫ASP.net

[英]URL Rewrite ASP.net

我有一個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));
        }
    }

}

當我從普通網址打開頁面時,它可以很好地打開,但是當我使用該網址重寫時,它打開卻沒有圖像或樣式。

當我打開螢火蟲時,出現錯誤,找不到CSS和javascript

若要使重寫與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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM