I am working with webforms and as such I have a website running on IIS 8 on a server 2012, using vb.net. We have some URL redirection happening on Global.asax, on the Application_BeginRequest event which opens an XML file and compares the incoming request to what it would be translated to in the XML. If the request cannot be satisfied by searching the XML, then we need to first raise a 410 status code, then redirect to a generic page. So, the way I've been trying to do this is
HttpContext.Current.Items.Add("Raise410", "true")
HttpContext.Current.Items.Add("Redirect", "/" + dv(0)("folder"))
Throw New HttpException(410, "Gone")
Then on the exception catch I do this:
Catch ex As Exception
If HttpContext.Current.Items("Raise410") IsNot Nothing Then
HttpContext.Current.Response.StatusCode = 410
HttpContext.Current.Response.Redirect(HttpContext.Current.Items("Redirect"))
End If
End Try
However all I am getting is a 302 redirect and the 410 is never raised. I am not sure why and I've tried to figure this out for several hours to no avail.
There's some design flaw in your approach. Both 410 Gone and 301 Redirect are HTTP status codes.
You can't return a response and then redirect from server code.
One possible approach is returning HTTP/410 from the server side and do the redirect in the client-side using META refresh or using a setTimeout and setting window.location to the URL where you want to redirect:
setTimeout(function() {
window.location = "http://";
}, 6000); // 6 seconds and do the redirect
Related
I use ASP.Net URL Routing for SEO and when I run into a routename that is bad in (i.e. /Games/{RouteName}) I want to pass it to my 404 page for handeling. I check the DB to make sure {RouteName} is a valid name for a game. If the RouteName is valid I want to continue processing as normal but if the DB comes back and does not find a corisponding RouteName I would like to some how have the server act as if a truly bad URL was entered. How can I best accomplish this?
I would suggest throwing a 404 error inside your logic that determines it is not valid, e.g.
if (!<validRouteName>) {
return new HttpStatusCodeResult(404, "Game not found.");
}
Doing a bit of messing around in IIS I have figure this out. As endyourif suggested I need to throw a 404 resposne in my code behind, which was done by Response.StatusCode = 404; I also had to configure IIS's Error Pages for the 404 to point to my 404.aspx page.
I am trying to use the jQuery plugin 'timeout-dialog' in my ASP.Net app to caution the user that his/her session is about to expire. This plugin is at: http://rigoneri.github.com/timeout-dialog.js/. It says that one of the parameters called 'keep-alive-url' is as below:
keep_alive_url -The url that will perform a GET request to keep the session alive. This GET expects a 'OK' plain HTTP response.
How would I create a page in ASP.Net or simple html that would return a plain 'OK' in its http response?
Try:
Response.StatusCode = 200;
in ASP.NET code behind.
This stumped me for awhile.
Some reason it needs to be a 404 Not Found
<cfheader statusCode="404" statusText="Not Found">
I have several web services that are being called using AJAX requests from Asp.Net page. If the user's authorization fails, i want to redirect him to the login page.
The problem i am facing is i have to manually redirect the user in the errorHandler function of AJAX request.
I was wondering if there is a way i can throw HttpException with proper error code from my webserivce (i dont know which) so that browser understands the response and redirects user to the login page?
I tried throw new HttpException(401,"Unauthorized access request); in my service call, but my errorHandler handles this exception and browser doesnt redirect user back to login page.
Also i was thinking if i can achieve this using HttpModule, will that be a better solution rather than having Authorization check in the service constructor?
--Update--
After adding HttpContext.Current.Response.RedirectLocation = "login.aspx"; and HttpContext.Current.Response.StatusCode = 307;, it now gives me an error saying "Resource cannot be found". This is what it shows in the RequestedURL: /Web/Services/svcSomething.asmx/login.aspx.
How do i tell browser that redirect location is a new page and not the webmethod?
IMO - you need to handle the error (401) on the server side & redirect the user to the login page. Since you don't have any handler on the server side, the error is passed on to the client as a response & so you are seeing the code on the client-side to redirect the user...
Our ASP application is moving to a new server and I want to implement a permanent URL redirection. I am aware of the following two approaches, but I need to understand which one to use and when.
Option 1:
<%# Language=VBScript %><% Response.Redirect "http://www.example.com" %>
Option 2:
<%# Language=VBScript %><% Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.example.com/" %>
Thanks,
Nikhil.
Response.Redirect issues a 302, which is a temporary redirect. 301, using the Response.AddHeader that you listed, is for permanent redirects.
The differences between 301 and 302 have some importance with search-engine-optimization. A 301 will hold all of your search rankings from the old location. On the flip side, if you DON'T want your new page to be indexed, you can use a Response.Redirect (302) since the engines will consider the redirect temporary. Google doesn't index 302's because a lot of spammers use it to try to increase their rankings.
Since you're permanently moving to a new server, a 301 is the best way to go.
Response.Redirect() (and the equivalent method RedirectPermanent() for a 301) does a lot of things behind the scenes. It null checks the requested URL string, encodes it, calls the event handlers for the Redirecting event if there are any, and finally calls Response.End(), which flushes the response back to the browser and aborts the current thread.
Ultimately, you probably won't notice much difference between setting headers manually and calling redirect.
Incidentally, there are more (and better) options for handling this. IIS has a URL Rewriting module, which would let you redirect a given URL without ever calling your page as a request handler, and centrally manage your URL's for easier management of search engines.
A normal redirect will by default use HTTP status 302. A redirect with status 301 will not be indexed by searchbots (like Googlebot) and if they were, they will be removed from existing indexes. Very useful if you want to "update" an old URL to a newer URL. The searchbot will index redirects with status 302 anyway, so you may likely end up with pollution in search results. You'd normally use status 302 for for example PRG pattern and status 301 for permanent redirects like as you're doing now.
Response.Redirect sends a "302 - moved temporarily" status code to the browser, which may or may not be okay depending on what you are doing. If you are redirecting to the correct location for your content, you want to do the 301 redirect because search engines will not crawl properly against a 302.
I have an ASP.NET HttpModule that handles HttpApplication.BeginRequest to redirect some requests to other files. Typically I'll do something like parse the request, write a file to HttpContext.Current.Response.OutputStream then call HttpApplication.CompleteRequest(). For some requests I can't find the relevant file - how do I return a 404 error?
Have you tried throw new HttpException(404, "Not Found");?