简体   繁体   English

ASP.NET ReturnURL 到特定搜索结果页面

[英]ASP.NET ReturnURL to specific search results page

I have an ASP.net site with C# backend code.我有一个带有 C# 后端代码的 ASP.net 站点。 We have the following (abridged) code in it, which suits our needs, but it could be better.我们有以下(删节)代码,它适合我们的需要,但可能会更好。 This is on a page called SearchResults.aspx.这是在名为 SearchResults.aspx 的页面上。 If the user is not logged in, the links will redirect them to the login page.如果用户未登录,链接会将他们重定向到登录页面。 If they are logged in, it will redirect them to a lookup page for that item.如果他们已登录,它会将他们重定向到该项目的查找页面。 What I want it to do is to redirect them to the corresponding item page after they log in if they click the "not logged in link".我想要它做的是,如果他们单击“未登录链接”,则在他们登录后将他们重定向到相应的项目页面。 In what way would I need to supply the returnURL to the login page?我需要以什么方式向登录页面提供 returnURL? Every way I've tried, it just redirects me to the default page after login.我尝试过的每一种方式,它都会在登录后将我重定向到默认页面。

<AnonymousTemplate>
    <!--Want to change the link below so that the return URL will take me to 
    ItemInformation.aspx-->
     <%# DataBinder.Eval(Container.DataItem, "itemnumber").ToString().Trim() %><br/>
     <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='Account/Login.aspx'>
     Please login to review information for this item.</asp:HyperLink>
</AnonymousTemplate>
<LoggedInTemplate>
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%#
       "~/ItemInformation.aspx?&ItemID=" + DataBinder.Eval(Container.DataItem,
       "itemnumber").ToString().Trim() + "&itemdept=" + DataBinder.Eval(
       Container.DataItem, "department").ToString()%>'
       Text='<%# DataBinder.Eval(Container.DataItem, "itemnumber")%>'>
    </asp:HyperLink>
</LoggedInTemplate>

Edit - I'm using the default login structure for an ASP.net Web Application template, so this is all that is in the Login backend.编辑- 我正在使用 ASP.net Web 应用程序模板的默认登录结构,所以这就是登录后端的全部内容。

    protected void Page_Load(object sender, EventArgs e)
    {
        string returnUrl = Request.QueryString["ReturnUrl"];
        RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" +
          HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
    }

On SearchResults.aspxSearchResults.aspx

if (!Request.IsAuthenticated)
{
    Response.Redirect("/Login.aspx/?ReturnURL="+HttpContext.Current.Request.Url.AbsoluteUri); // dont forget to use urlencode
}

On Login.aspxLogin.aspx

protected void Login_Click()
{
    if (Request.QueryString["ReturnURL"] != null)
    {
        Response.Redirect(Request.QueryString["ReturnURL"]);
    }
    else
    {
        Response.Redirect("/Home.aspx");
    }

}

In one of my attempts to try to get it to work, I was only attempting to set the DestinationPageUrl attribute for my asp:Login control.在我尝试让它工作的一次尝试中,我只是尝试为我的 asp:Login 控件设置 DestinationPageUrl 属性。 For some reason, it required me to use both this and the OnLoggedIn event together (I was not aware of this events existence before Zerkey pointed it out).出于某种原因,它要求我同时使用 this 和 OnLoggedIn 事件(在 Zerkey 指出它之前我不知道这个事件的存在)。 The Additional question mark in the return URL was also causing issues, so here's what I did in Login.aspx.返回 URL 中的附加问号也引起了问题,所以这是我在 Login.aspx 中所做的。

Markup:标记:

<asp:Login ID="LoginUser" runat=server" EnableViewState="false"
       RenderOuterTable="false" OnLoggedIN="UserLoginOnLoggedIn">...</asp:Login>

Code:代码:

protected void UserLoginOnLoggedIn(object sender, EventArgs e)
    {
        string itemid, itemdept;
        try
        {
            s1 = Request.QueryString["ItemID"].Trim();
            s2 = Request.QueryString["Dept"].Trim();
        }
        catch
        {
            //makes strings null if querystrings aren't present
            s1 = "";
            s2 = "";
        }
        string returnUrl = Request.QueryString["ReturnUrl"] + "&ItemID=" +
            Request.QueryString["ItemID"] + "&Dept=" +
                        Request.QueryString["Dept"];
        if ((!String.IsNullOrEmpty(returnUrl) && !String.IsNullOrEmpty(s1) && 
        !String.IsNullOrEmpty(s2)))
            LoginUser.DestinationPageUrl = returnUrl;
        else
            LoginUser.DestinationPageUrl = "~/Default.aspx";
        Response.Redirect(LoginUser.DestinationPageUrl);
    } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM