【问题标题】:Redirect to different URL and show another URL(page in this URL Does Not Exist) in address bar in ASP.net [duplicate]重定向到不同的 URL 并在 ASP.net 的地址栏中显示另一个 URL(此 URL 中的页面不存在)[重复]
【发布时间】:2018-11-26 22:18:48
【问题描述】:

我有一个重定向到 "/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>

通过这个我匹配我的 URL,如果它包含“certifications”然后它重定向到“details.aspx”页面,查询字符串键为“url”,其中包含原始 URL。但是我的 URL 显示了我重定向到的页面。

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

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

请帮忙。提前谢谢!!!

【问题讨论】:

  • @mjwills 我当前的页面是“elist.asp”,我希望它重定向到“details.aspx”,但地址栏应该显示“/Certification/abc123.asp”。您建议的链接在这种情况下不起作用。
  • elist.asp 应该是 Response.Redirect/Certification/abc123.asp/Certification/abc123.asp 应该是 Server.Transferdetails.aspx
  • @mjwills。 /Certification/abc123.asp 此页面不存在。它只是 url,并且有成千上万个这样的 url。所以我们在detail.aspx中有通用模板。基于查询字符串“url”,我们想要显示数据。希望您能理解我的要求。
  • This 可能有帮助
  • @AjayGupta Server.transfer 将不起作用。因为我想将 URL 从 elist.asp 更改为 /Certification/abc123.asp。我想你错过了我的 Note。 /Certification/abc123.asp 页面不存在。它只是我想向用户显示并重定向到 detail.aspx 的 URL

标签: c# asp.net url-rewriting


【解决方案1】:

这段代码解决了我的问题。

全球.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);

        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2018-06-01
    • 2014-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多