protected void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
static void RegisterRoutes(RouteCollection routes)
{
//routes.MapPageRoute("Uniquename", "Name to shown on Adddress bar AND for redirecting", "Physical Path to the page");
routes.MapPageRoute("Home", "StoreFrontPage", "~/BestSeller.aspx");
routes.MapPageRoute("index", "MainPage", "~/index.aspx");
routes.MapPageRoute("ProductDetails", "DetailOfProduct", "~/ProductDetails.aspx");
}
Above codes helps me change the name of the url when i use "href" or "response.redirect" so when i open the first webpage (the starting page) i could not hide the url.
i do not want users to see the name of the webpage.aspx file.
thanks for any advices/help!
Related
I am trying to pass data from a textbox in one page to a text block in the navigated page. I have some code but I am finding an error when running it here is my coding.
From the page I want to send the data from:
private void button1_Click(object sender, RoutedEventArgs e)
{if (txtSID.Text != null)
{
string StudentID = txtSID.Text;
var url = string.Format("/BookingConf.xaml?StudentID={0}", StudentID);
NavigationService.Navigate(new Uri(url, UriKind.Relative));
}
Code from the Navigated Page:
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
String StudentID;
if (NavigationContext.QueryString.TryGetValue
("studentID", out StudentID))
{// load event data, and set data context
ReferanceST.Text = StudentID;
}
}
The issue is that when I run the application I get an error on the 'OnNavigationTo(NavigationEventArgs e)' saying no suitable method found to override.
In order to fulfil this i placed the 'if' statement but it made no difference.
Please help me resolve this issue. Thank you.
The OnNavigatingTo takes the NavigationEventArgs, not the NavigatingEventArgs.
Change your line to:
protected override void OnNavigatedTo(NavigationEventArgs e)
The error is happening because you miss named the override method.
The error is the smoking gun
"no suitable method found to override"
To fix this
protected override void OnNavigationTo(NavigatingEventArgs e)
{
should be
protected override void OnNavigatedTo(NavigatingEventArgs e)
{
MSDN Reference
I was using Form Authentication in my test. And also have some test user name .But found a weird problem for a specified name. That is all of test names except only one named amybeyond can works in the test.
Please help to review my code in my test.
LoginTest.aspx (This is a login form for user name and password input.)
public partial class LoginTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//after succeed validating user. then redirect to LoginSuccess.aspx page.
bool bValidate=Membership.ValidateUser("amybeyond", "11111111");
if (bValidate)
{
FormsAuthentication.SetAuthCookie("AmyBeyond", false);
Response.Redirect("LoginSuccess.aspx");
}
}
}
LoginSuccess.aspx (In this page, just simply test if current request is authenticated after redirecting.)
public partial class LoginSuccess : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//the HttpContext.Current.Request.IsAuthenticated always false in the IE.
if (HttpContext.Current.Request.IsAuthenticated)
{
Response.Write("ok, you login successfully.");
}
}
}
I am sure the Membership.ValidateUser is successfully executed and return true. The problem is it can't know the authenticated status after successfully redirecting.
I didn't know if I miss something or did something wrong. If there is . Please help to tell me .thanks.
Added
I read the source code of FormsAuthentication.SetAuthCookie. and add the cookieless="UseCookies" in the Forms element of the Web.config. Hope to make sure the cookie is added to the Response(This is done by the source code HttpContext.Current.Response.Cookies.Add(cookie)). Still doesn't work.
public static void SetAuthCookie(string userName, bool createPersistentCookie, string strCookiePath)
{
Initialize();
HttpContext current = HttpContext.Current;
if (!current.Request.IsSecureConnection && RequireSSL)
{
throw new HttpException(SR.GetString("Connection_not_secure_creating_secure_cookie"));
}
bool flag = CookielessHelperClass.UseCookieless(current, false, CookieMode);
HttpCookie cookie = GetAuthCookie(userName, createPersistentCookie, flag ? "/" : strCookiePath, !flag);
if (!flag)
{
HttpContext.Current.Response.Cookies.Add(cookie);
current.CookielessHelper.SetCookieValue('F', null);
}
else
{
current.CookielessHelper.SetCookieValue('F', cookie.Value);
}
}
Added
The http capturing detail shows below. in the LoginTest.aspx there is a cookie named FwLoginCookie , after redirect to LoginSuccess.aspx this cookie is lost. please help to review it .
Finally got why did this weird thing happen! It is because there is an another cookie named ACA_USER_READ_ANNOUNCEMENT sent to response. It is so large size (more than 5800bytes) that the browser (in my test it is IE) would ignore all the cookies include the Form authentication cookie(about 300bytes).
But other browser like chrome/firefox is not the same behavior with IE when encounter this case (huge cookie size.).
If it is not right . Please kindly correct me . Thanks.
I have created a web browser in c#
this was what i get when i opened my web browser and typed google. Then i searched google for something
the result was like this
But the url wasn't updated in address bar. How to update the address bar when user click on a link on any website in my web browser
In the first image the url was google.com
In the second image url was https://www.google.co.in/#hl=en&output=search&sclient=psy-ab like that some thing but it wasn't updated
You must update the textbox on top with the URL of the WebBrowserControl, using the webBrowser1_Navigating event.
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
textbox1.text = webBrowser1.Url.ToString();
}
Check http://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser_events.
I think you can use Navigating event to detect when user starts search or navigates to another page.
The Form_Load must contain this:
private void Form1_Load(object sender, EventArgs e)
{
web = new WebBrowser();
web.Navigated += web_Navigated;
}
and this function:
private void web_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = web.Url.ToString();
}
Hi all I have seen many articles on url rewriting but I didn't find any as per my requirement. Assume I have two pages Default.aspx and Default1.aspx.. On initial load I would like to re write my Default.aspx to some thing like urlrewrite\dummy.aspx and on my Default.aspx I will have a button when I click on this I am going to redirect to Default1.aspx I would like to rewrite this to urlrewrite\dummy1.aspx
I just post the sample rewrites but if there is any better way of redirecting can you please help me..
Also what is the best way to rewrite all pages if I have some 20-50 pages
my global.asax file
<%# Application Language="C#" %>
<%# Import Namespace="System.Web" %>
<%# Import Namespace="System.Web.Routing" %>
<script RunAt="server">
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routeCollection)
{
string root = Server.MapPath("~");
System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(root);
System.IO.FileInfo[] files = info.GetFiles("*.aspx", System.IO.SearchOption.AllDirectories);
foreach (System.IO.FileInfo fi in files)
{
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
routeCollection.MapPageRoute(fi.Name + "Route", fi.Name, pageName);
}
routeCollection.MapPageRoute("DummyRouteName1", "Dummy", "~/Default2.aspx");
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
You can add routes in your Global.asax file on application start:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("DummyRouteName", "Dummy", "~/Default.aspx");
....
}
Usage:
Response.Redirect("~/Dummy");
In url you will see: (server)/Dummy
Edit:
here is how to automatically add routes:
// Get root directory
string root = Server.MapPath("~");
DirectoryInfo info = new DirectoryInfo(root);
// Get all aspx files
FileInfo[] files = info.GetFiles("*.aspx", SearchOption.AllDirectories);
foreach (FileInfo fi in files)
{
// Get relative path
string pageName = fi.FullName.Replace(root, "~/").Replace("\\", "/");
// Add route
routes.MapPageRoute(fi.Name + "Route", fi.Name.Replace(".aspx", ""), pageName);
}
I assume that you got that rewriting part covered and only problem is postback, you can set postback to "friendly" URL by seting form action, like this :
Page.Form.Action = Page.Request.RawUrl;
I have added the following code to my Global.asax file:
<%# Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
etc.....
But my BeginRequest function just gets ignored. How do I redirect my entire application from http: to https:?
If you're using a master page or a base class, I would put your logic there. Global events shouldn't be relied upon for logic like this.
Put the logic in Page_Load (or earlier in the lifecycle) of the master page or base class like this:
protected void Page_Load(object sender, EventArgs e)
{
if (ConfigurationManager.AppSettings["IsReviewServer"] == "Yes")
{
if (!Request.IsSecureConnection)
{
string path = string.Format("https{0}", Request.Url.AbsoluteUri.Substring(4));
Response.Redirect(path);
}
}
}
You could do the above at another point in the lifecycle if you wanted too, like PreLoad or PreRender.
Using global events
If you're going to use a global event, I would actually use Application_EndRequest, because it gets called on every request so the application can clean up resources.