I am using ASP.NET Friendly URLs with success, but I need to ignore route for a particular Foo.aspx page (because this page needs POST data and once re-routed the POST data is not available anymore in Page_Load()!).
It looks like using ASP.NET Friendly URLs discard any attempt to ignore a route. Even the MSDN example for ignoring route doesn't work once ASP.NET Friendly URLs routing is used:
routes.Ignore("{*allaspx}", new {allaspx=#".*\.aspx(/.*)?"});
And to ignore route to Foo.aspx the code should look like that, isn't it?
routes.Ignore("{*fooaspx}", new { fooaspx = #"(.*/)?foo.aspx(/.*)?" });
The Global.asax code looks like:
public static void RegisterRoutes(RouteCollection routes) {
// This doesn't work whether I put this code before or after ASP.NET Friendly URLs code.
routes.Ignore("{*allaspx}", new { allaspx = #".*\.aspx(/.*)?" });
routes.Canonicalize().Lowercase();
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
}
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
This question has been asked on the ASP.NET Friendly URLs codeplex site, but didn't get an answer.
Thanks for your help on this :)
Thanks to Damian Edwards comment, I got this issue completely solved, thanks Damian.
I just need to derive from WebFormsFriendlyUrlResolver to override the method ConvertToFriendlyUrl() to make it no-op when the url match the url I don't want to redirect:
using Microsoft.AspNet.FriendlyUrls.Resolvers;
public class MyWebFormsFriendlyUrlResolver : WebFormsFriendlyUrlResolver {
public MyWebFormsFriendlyUrlResolver() { }
public override string ConvertToFriendlyUrl(string path) {
if (!string.IsNullOrEmpty(path)) {
if (path.ToLower().Contains("foo")) { // Here the filter code
return path;
}
}
return base.ConvertToFriendlyUrl(path);
}
}
Then in Global.asax the code now looks like:
public static void RegisterRoutes(RouteCollection routes) {
routes.Canonicalize().Lowercase();
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings,
new IFriendlyUrlResolver[] {
new MyWebFormsFriendlyUrlResolver() });
}
void Application_Start(object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
This was interesting - had to tinker :) In my comment above, what I was trying to say was "no need to ignore".
I was "right" and "wrong".
right: no need to ignore
wrong: not because of what I stated (re: physical file), but rather, by not invoking the redirect in the first place.
This will bomb out (redirect will occur, POST data is lost):
<asp:Button ID="btn1" runat="server" Text="Go" PostBackUrl="~/Target.aspx" />
This will be good (you will get POST data, no redirect occurs):
<asp:Button ID="btn1" runat="server" Text="Go" PostBackUrl="~/Target" />
The difference? I'm not asking FriendlyUrls to "re-route" anything in the 2nd option. In the first option, I'm asking for an "aspx" file, so FriendlUrls will dutifully do its purpose for being and "handle" it (and do a permanent redirect to a "friendly url" which is a GET and there goes all the POSTed data).
Inspect request in 1st option (target.aspx):
Inspect request in 2nd option (extensionless, target):
This was a clue:
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
And it does what it says, "do a permanent redirect" (to a "friendly url")...when "necessary" (if an "aspx" file is requested). You can tinker with this with any page in your WebForms site
if you request foo.aspx you will see a Redirect (to foo)
if you request foo, no Redirect
You can also comment out
settings.AutoRedirectMode = RedirectMode.Permanent;
and things will work but sort of defeats the purpose of FriendlyUrls...
Thinking about it, it makes perfect sense. There is no need to "redirect" on every request (ugh for performance), rather only if/when necessary...
Hth....
Related
I'm creating a hybrid aspx/mvc application and want to route from the aspx code behind to an mvc controller. I am missing something... To keep this simple: I have a button in my aspx page:
<asp:Button ID="Attendees" runat="server" OnClick="Attendees_Click"/>
to my code behind:
protected void Attendees_Click(object sender, EventArgs e)
{
//How can this be redirected this to my Attendee/Index Controller?
//Url.Redirect("Index","Attendee");//This does not work?
Response.Redirect();
}
Usually by mentioning the path of controller name and action name should work:
Response.Redirect("~/Attendee/Index");
However if the above way doesn't work, the best way is using UrlHelper instance to create target URL (with UrlHelper.Action() overload) and redirect afterwards:
var url = new UrlHelper(HttpContext.Current.Request.RequestContext);
Response.Redirect(url.Action("Index", "Attendee"));
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>
Is it possible to make a asp.net website available only during a particular time...?
How to implement it..?
Yes you can do that using the BeginRequest on Global.asax
protected void Application_BeginRequest(Object sender, EventArgs e)
{
if(!(DateTime.UtcNow.Hour >= 9 && DateTime.UtcNow.Hour <= 17))
{
HttpContext.Current.Response.TrySkipIisCustomErrors = true;
HttpContext.Current.Response.Write("Even if we are web site, we are open from 9.00 to 17.00 Only! :) <br />Ps If you are the google spider leave a message under the door.");
HttpContext.Current.Response.StatusCode = 403;
HttpContext.Current.Response.End();
return ;
}
}
Depending on what you want the site to look like when it is down you could do it in different ways. One example would be to create a BasePage class and add a code to return 404 or redirect to error page when the site should be down. Another option is to subscribe for Application_BeginRequest event in Global.asax and do the same thing there.
In your global.asax
Pseudo code:
Session_Start()
{
If(!CurrentTime in DesiredTimeFrame)
{
Redirect to somewhere sensible. Maybe HTML page explaining why not available.
}
}
I want to be able to globally grab a reference to the Page object that is currently being requested, so I've added a Global.asax file.
Is there any way to do this? I assume it has something to do with the Application_BeginRequest event.
You can access the current handler (the page) from global.asax, but not from any stage of the request life cycle. I.e. it is not possible in BeginRequest, but it is possible during PreRequestHandlerExecute:
void Application_PreRequestHandlerExecute(object sender, EventArgs e)
{
var page = (Context.Handler as System.Web.UI.Page);
}
Note that page might still be null, if the handler is not a page. Also, I'm not sure your approach is the correct one. Maybe you should explain in more detail what you want to attempt?
Create a class that is a subclass of Page that does what you want and use this subclass as the base type for all you pages.
public class MyPage : Page
{
//... override whatever you want, add functionality, whatever
}
All other pages:
public class Index : MyPage
{
// Automatically get new behaviour
}
You have to use http module to catch the every request for each page on your application and do whatever you want with the request.
var page = HttpContext.Current.Handler as Page
if(page != null) /// do something with page
PreRequestHandlerExecute should be fine for your purposes (if you don't fancy writing your own HttpModule, which is in fact very easy)
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).