How I can hide url - asp.net

void Application_BeginRequest(Object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app != null)
if (app.Request.AppRelativeCurrentExecutionFilePath == "~/About.aspx")
app.Context.RewritePath(
app.Request.Url.PathAndQuery.Replace("/About.aspx", "")
);
}
I want to hide url like the following :
requested url : "http://domain.com/WebFolder/page.aspx"
url should be show : "http://domain.com/WebFolder"
The above code is not producing my desired result. It's causing to redirect new rewrite page. I don't want that, I just want to hide my url for section of page name.

You can use the IIS URL Rewrite module.

I have two ways to accomplish this in mind.
One is making your page a default page under IIS.
To do this,you should select Properties page for your folder WebFolder under IIS then select default page ( default is index.html ) to your page (page.aspx),then make it top page.
Second is ISAPI Rewriting which is more useful.It is a free .dll.Look at this site for more information.
Hope this helps
Myra

Related

ASP.NET code source updating

I built a website and I want to redirect a site built in asp.net
( similar example www.mysite.net/Front/ContactUs.aspx?Page=ContactUs&mn=ContactUs) to my new site.
the problem is that I have not found where I can make changes in aspx code, I looked into the source code but in vain.
The project contains a lot of files of code, I do not really know what part of the code I have to show you.
Is ASP's projects have a syntax to define the site URLs?
Please I need help, how to make this redirection , and where I can make changes.
Thanks.
Whether you want to redirect all requests or just select individual files, partially or fully, they all normally have a function called Page_Load.
For every file you want to be redirected, you can use this code piece.
private void Page_Load(object sender, EventArgs e)
{
// Check whether the browser remains
// connected to the server.
if (Response.IsClientConnected)
{
// Redirect
Response.Redirect("http://new.website.com/", false);
}
else
{
// Browser is not connected, stop all response processing
Response.End();
}
}
If you want the full site to be redirected, you can use the this function, in global.asax file
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
Response.Redirect("http://new.website.com/", false);
Response.End();
}
</script>

Redirect an URL by default, and show the original page when ?redirect=false is shown

I have a question about URL redirection.
Now I have the .aspx file of a page. And I have added the following part at the beginning of the .aspx file:
<script runat="server">
protected override void OnLoad(EventArgs e)
{
Response.Redirect("New URL");
base.OnLoad(e);
}
</script>
So the old URL is redirected to the new one. This part works well.
But when I add a ?redirect=false at the end of the old URL, it still redirects me to the new page.
Is there a way that I can see the old page?
Well... you actually need to test for that query parameter.
something like:
if ((Request.QueryString["redirect"] == null) || (Request.QueryString["redirect"] != 'false'))
Response.Redirect("NEW URL");
}
base.OnLoad(e);
The test for null is there in case the redirect parameter wasn't passed in.

Wrong page after Login

If I click on a link(Without logging into the site) I am navigated to this page:
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fContacts.aspx
When I login to the site I want to be redirected to my Home page rather than Test Contacts.aspx page.
How can I resolve this?
If i do this then I'm not logged in.
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Login1_LoggingIn(object sender, LoginCancelEventArgs e)
{
Response.Redirect("Home");
}
Just change link url you clicking on to:
http://localhost:59196/Login.aspx?ReturnUrl=Index.aspx
I think you clicked on Test Contact.aspx page with out logging in and hence you have been redirected to the Login page with a query string as ReturnURl = test contact.aspx, which might be used to redirect from the login page.
So instead of using return url query string you can redirect directly to your page page from the log in button click in the login page.
Hope it works.
I mean in
Loginbutton_click()
{
//login validation code here
//after successful validation
Response.redirect("Yyourhomepageurlhere");
}
You want to redirect to
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fIndex.aspx
rather than
http://localhost:59196/Login.aspx?ReturnUrl=%2fTest%2fContacts.aspx
In other words the link that you click on should point to the first link above rather than the second.
The question that remains is how are the links generated? Are they hard coded or are you using MVC and generating the link somehow? Is the link even in your control to generate?
It would appear your code looks at the ReturnUrl query string parameter and on successful login redirects to it.
Did you come to the Login page from the Contacts page? If so then the ReturnUrl might have been generated dynamically depending on where you came from. You may require this to be fixed to being the Index page (I assume this is what you have called it). Be careful though, other developers may want to have this link be dynamic.
I must changed the event.
protected void Login1_LoggedIn(object sender, EventArgs e)
{
Response.Redirect("Home");
}

how to programmatically decide which content page to load , on master page load

I have a master page and two content pages with the same ContentPlaceHolderID. Is there a way to specify which content page should be loaded from the Page_Load event of the master page?
If I watch the value of:
Request.CurrentExecutionFilePath;
I see the path of the first content page.
According to the condition specified below i want to change it to the path of the second content page.
I am looking for a way to load a specific page depending on a check I do on the Master Page_Load.
If I attempt to redirect to the page from there I get stuck in an endless loop because the master page loads again before the content page and re-does the check and redirects again and again.
// in master page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Request.Cookies["user"] != null)
{
HttpCookie cookie = Request.Cookies["user"];
string name = cookie.Value;
Response.Redirect("~/hello_page.aspx?UserName=" + name);
}
}
}
Thanks in advance.
You could check on page load for the querystring you're including in the redirect. If the querystring is there, then you will have already redirected, so you can skip over the cookie-check-and-redirect block.

Recursive redirect on error handler page

I use the following MSDN code on my master page to detect if the browser accepts cookies:
protected void Page_Load(object sender, EventArgs e)
{
if(!this.IsPostBack) {
if(Request.QueryString["CookieTest"] == null) {
Response.Cookies["TestCookie"].Value = "Test";
Response.Redirect("CookieCheck.aspx.redirect=" + Server.UrlEncode(Request.Url.ToString())));
}
else if((string)Request.QueryString["Test"] == "passed") {
// my stuff...
}
}
}
The CookieCheck.aspx contains the following:
protected void Page_Load(object sender, EventArgs e)
{
if(Request.Cookies["TestCookie"] == null)
Response.Redirect(Request.QueryString["redirect"] + "?Test=notPassed", true);
else
Response.Redirect(Request.QueryString["redirect"] + "?Test=passed", true);
}
Within the web.config i have defined the following:
<customErrors mode="On" defaultRedirect="Error.aspx" />
Now the recognizing of the cookies works well, but I have this problem: Whenever an error occurs on the page and I should be redirected to Error.aspx (and this worked before the whole cookie detection thing), the redirection seems stuck in an infinite loop and appends more and more "?Test=passed" to the URL. I should mention that the Errors.aspx also has the same masterpage and thus also performs the cookie check. However I have no clue why the redirection doesn't stop. Is there a way to solve this problem other than to exlude the Errors.aspx page from having the master page? Thank you very much.
If the CookieCheck.aspx page also uses the same Master page it will keep redirecting recursively, make sure that CookieCheck.aspx is not using the same MasterPage.
I'd rather recommend not using MasterPages for this, Master Pages by design are for Visual Inheritance not code Inheritance, if you wish to make some special type of pages that checks for the the browser ability to use cookies, you can have a new base class for these pages
public abstract class CookieEnabledPage : Page
{
}
and add your logic to this class, then whenever you need to make a new page with this behavior you inherit from this base class. I think this is a much cleaner way of doing what you want.
I guess the masterpage (or the combination masterpage-error.aspx) is raising an exception which triggers a redirect to error.aspx, which in turn causes the masterpage to restart its lifecycle and raise a new exception. The concatenation of "?Test=passed" is almost certainly a side effect of reinvoking the cookie test every time an error redirect occurs.
I suggest firing up the debugger and setting a breakpoint at Page_Load in Masterpage.aspx.cs and step through until you are redirected to the error page (the last line which gets execued is the one raising the exception).

Resources