重定向到不同的URL并在ASP.net的地址栏中显示另一个URL(此URL中的页面不存在)

时间:2018-06-18 05:42:03

标签: c# asp.net url-rewriting

我有一个重定向到“/ Certification / abc123.asp”的链接。现在我想要的是页面应该重定向到另一个页面“details.aspx?URL = / Certification / abc123.asp”但地址栏中的URL应该出现“/ Certification / abc123 .asp“。我在web.config中创建了Url重写规则。

以下是我的代码:在web.config

 <rule name="Redirects to Certifications" stopProcessing="true">
      <match url="(.*certifications*)"/>
      <conditions>
        <add input="{HTTPS}" pattern="^OFF$"/>
      </conditions>
      <action type="Redirect" url="http://{HTTP_HOST}/ERRoot/details.aspx?url={REQUEST_URI}"  appendQueryString="true"/>
    </rule>

按此我匹配我的网址,如果它包含“认证”,则会使用查询字符串键“网址重定向到” details.aspx “页面strong>“包含原始网址。但是我的URL显示了我重定向到的页面。

我想在我的地址栏中显示“/Certification/abc123.asp”。

注意:没有像/Certification/abc123.asp这样的网页。它只是一个URL

请帮忙。在此先感谢!!!

1 个答案:

答案 0 :(得分:0)

此代码解决了我的问题。

的Global.asax:

private void Application_BeginRequest(object sender, EventArgs e)
{
if (System.IO.File.Exists(Request.PhysicalPath))
    {
        // Do nothing here, just serve the file
    }
    // If the file does not exist then
    else if (!System.IO.File.Exists(Request.PhysicalPath))
    {
        if (Request.ServerVariables["URL"].ToString() != "/")
        {
            // Get the URL requested by the user
            string sRequestedURL = Request.Path;

            string varurl = Request.ServerVariables["URL"].ToString().ToLower();
            string sTargetURL = "";

            if (varurl.Contains("/certifications/"))
            {
                sTargetURL = "~/Erroot/details.aspx?url=" + varurl;
            }

            if(sTargetURL!="")
            Context.RewritePath(sTargetURL, false);

        }
    }
}