如何在ASP.NET中添加一些条件代码重定向到当前页面 [英] How to add some conditional code for redirect to current page in ASP.NET

查看:87
本文介绍了如何在ASP.NET中添加一些条件代码重定向到当前页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。我在asp.net中有一个Confirm-user.aspx绑定到母版页。确认用户页面在文件夹(Manage_pages)中。我在母版页中有侧边栏。并使用此代码列出用户可以访问的所有页面:



Hi all.I Have a Confirm-user.aspx in asp.net bound to master page.the Confirm-user page is in folder(Manage_pages).I have the sidebar in master page. and use this code for list all page that user can access:

<div id="sidebar">
                    <ul>
                        <li>
                            <h2>title</h2>
                            <ul>
                            <% foreach (DAL.Page page in pageList)
         {%>
         <% Response.Write("<li><a href="+page.PageAddress+">"+page.PageName+"</a></li>"); %>
        <% }%>


                            </ul>
                        </li>
                    </ul>
                </div>







问题是当我点击确认 - 用户页面链接我成功转到此页



http:// localhost:2112 / Manage_pages / ConfirmUser.aspx



。但是当我再次点击链接时它不起作用并转到:



http:// localhost:2112 / Manage_pages / Manage_pages / ConfirmUser。 aspx



请帮帮我。谢谢。



我尝试了什么:



如何添加一些在asp.net中重定向到当前页面的条件代码




the problem is that when I click on Confirm-user page link I go to this page successfully

http://localhost:2112/Manage_pages/ConfirmUser.aspx

.But when I click on link again it does not work and go to :

http://localhost:2112/Manage_pages/Manage_pages/ConfirmUser.aspx

please help me. thanks.

What I have tried:

how to add some conditional code for redirect to current page in asp.net

推荐答案

Response.Write("<li><a href="+page.PageAddress+">"+page.PageName+"</a></li>");





如果 page.PageAddress是Manage_pages / ConfirmUser.aspx那么这是一个相对网址,即相对于当前页面,所以Manage_Pages是当前页面以下的级别。如果您在主页上,那么这就是您所期望的Manage_pages / ConfirmUser.aspx。但是,当您使用ConfirmUser时,该页面将被视为该文件夹的子项,因此您正在查看Manage_pages / Manage_pages / ConfirmUser.aspx。



你想要的是一个绝对的引用,总是被解释为从网站的根目录,并且可以在最简单的地方完成,例如





If "page.PageAddress" is "Manage_pages/ConfirmUser.aspx" then that's a relative url, ie relative to the current page, so "Manage_Pages" is a level below the current page. If you're on the home page this is "Manage_pages/ConfirmUser.aspx" as you'd expect. However when you're on ConfirmUser then the page is considered a child of that folder, so "Manage_pages/Manage_pages/ConfirmUser.aspx" which is what you're seeing.

What you want is an absolute reference that is always interpreted as from the root of the site and that can be done at it's simplest like

Response.Write("<li><a href=/"+page.PageAddress+">"+page.PageName+"</a></li>");





我在网址的前面添加了一个/,然后将你的网址转换为/Manage_pages/ConfirmUser.aspx,它会从中查找Manage_pages文件夹网站的根源,无论你在哪里。



有更好的方法,比如使用Page.ResolveUrl或者有像

这样的东西

VirtualPathUtility.ToAbsolute Method(String)(System .Web) [ ^ ]



你的代码将如果您的网址中有空格也会出现问题。我会在生成url列表时完成所有这些工作,对它们进行操作以确保它们是绝对的,而不是在aspx页面上编写此代码。



I added a "/" to the front of the url and that turns your url into "/Manage_pages/ConfirmUser.aspx" which looks for the Manage_pages folder from the root of the website no matter where you are.

There are better ways though such as using Page.ResolveUrl or there are things like

VirtualPathUtility.ToAbsolute Method (String) (System.Web)[^]

your code will also have problems if there are spaces in your url. I would do all this work at the point that the list of urls is generated, do work on them to make sure they are absolute then rather than writing this code on the aspx page.


这篇关于如何在ASP.NET中添加一些条件代码重定向到当前页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆