Reputation: 20001
I have an old website based on asp.net webform. I need to change the url to friendly using the same site and also keep support for old url.
For this i thought of using HTTP Handler so that i can intercept the old url and redirect them to correct page with new url
example
old url: /news.aspx?newsID=10
new url: /news/10/title-of-the-news
Using handler file URLHandler.ashx
i want to redirect page to right page or recreate the new url.
what i did so far
create URLHandler.ashx
wrote logic to redirect to new url or let us say i created the new URL and redirected the page
registered the handler in we.config
This is not working for me when i run /news.aspx?newsID=10
first it should execute handler file and the redirect to the page based on the url.
I am not sure if i have done something wrong.
web.config
<system.webServer>
<!-- Un Comment This Part on web server Un Comment This Part on web server -->
<handlers>
<add name="url-handler" verb="*" path="*.aspx" type="URLHandler" resourceType="Unspecified" />
<add name="ChartImg" path="ChartImg.axd" verb="GET,HEAD,POST" type="System.Web.UI.DataVisualization.Charting.ChartHttpHandler, System.Web.DataVisualization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
</handlers>
<validation validateIntegratedModeConfiguration="false" />
</system.webServer>
SAMPLE Handler file code
<%@ WebHandler Language="C#" Class="URLHandler" %>
using System;
using System.Web;
public class URLHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
// context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
HttpResponse r = context.Response;
r.Write(context.Request.Url.AbsoluteUri);
}
public bool IsReusable {
get {
return false;
}
}
}
Upvotes: 2
Views: 3385
Reputation: 45771
Place the code in a .cs
file. I've reproduced your project (with the code in an .ashx
file) and received the following error:
Could not load type 'URLHandler'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Having the code in a .cs
file in App_Code
resulted in it working as you expected. I suspect that this is something to do with the fact that code inside .ashx
files gets compiled slightly differently to account for the fact that it's responding to a specific URL (URLHandler.ashx
) as it's a physical file, rather than being a class that's referenced from web.config
which may be sufficient to mangle the name/location of the type so IIS can't find it when referenced in config.
Upvotes: 1