asp.net page handlers - asp.net

i want to add some httphandlers for aspx pages by code via a http module.
is that possible? if it is, how?
thanks your advance..

Inherit IHttpModule, override Application_BeginRequest perform your rewrite logic and rewrite the URL with:
private void Application_BeginRequest(Object source, EventArgs e) {
((HttpApplication)source).Context.RewritePath(...);
}
Then register it in web.config with:
<httpModules>
<add name="UrlRewriteHandler" type="namespace.UrlRewriteHandler,project"/>
</httpModules>
Hope that helps.

Related

How to use Url route url with parameters

i have a page with ~/x.aspx with urlmappings :
<add url="Home" mappedUrl="~/x.aspx" />
what i want is when calling ~/x.aspx?type=y then url still display Home
is there any way to do that
<add url="Home" mappedUrl="~/x.aspx" />
<add url="Home" mappedUrl="~/x.aspx?type=y" />
If you're using Web Forms, you can use the following tutorial. Basically the "type" could be a list of optional check boxes and the complete URL with parameters could be constructed in your code-behind.
Walkthrough: Using ASP.NET Routing in a Web Forms Application
For MVC, see the following question:
Routing with Multiple Parameters using ASP.NET MVC
I haven't work with mappings in the web.config but apparently is not possibly to use wildcards/regex
But you can do that by overwriting in your Global.asax the method Application_Start
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
// routing segment variable: {}
routes.MapPageRoute(null, "home", "~/Pages/x.aspx");
routes.MapPageRoute(null, "home/{type}", "~/Pages/x.aspx");

how to solve mistake in url rewriting?

I'm using Intelligencia.UrlRewriter to rewrite url in web form asp.net.
I have a patch that rewrites from url.com/page.aspx?id=10 to url.com/page/10/
To rewrite I'm using the following code:
<rewriter>
<rewrite url="~/page/([0-9]+)/?$*[/]" to="~/Page.aspx?Id=$1"/>
This method is working well, but here is a mistake:
When I try this path :
url.com/page/10/dada/asd/asda/da/sd/etc../
I will see contents of
url.com/page.aspx?id=10
And this is not good for seo.
i want this:
redirect from :
url.com/page/10/dada/asd/asda/da/sd/etc../
To
url.com/page/10/
How can I solve this?
Use Routing for this purpose:
First of all, create Global.asax file.
Add following code to the heading part of the Global.asax:
<%# Import Namespace="System.Web.Routing" %>
Then change Application_Start method to the following:
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
And add RegisterRoutes method:
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "Page/{page_id}", "~/Page.aspx");
}
In your Page.aspx use the next code to read the route value:
if (Page.RouteData.Values["page_id"] != null){
//do anything what you need. For example, ShowPageValuesByPageID(Page.RouteData.Values["page_id"].ToString());
//ShowPageValuesByPageID is your method
}

IIS - How to redirect

How can I redirect www.example.com/foo to www.example.com\subdir\bar.aspx in IIS?
Note: a file called "foo" does not exist. I'd like to just redirect a URL with that pattern to the second URL
The best thing to do is not redirect, but instead use routing to map the URL to the web form. Then you keep a nice clean URL which is easy for users to type, and it looks better for search engines. We can accomplish that with the MapPageRoute method.
Add this to your Global application class (global.asax or global.asax.cs)
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "foo", "~/subdir/bar.aspx");
}
Alternatively, you can add this to your web.config to do a redirect.
<configuration>
<location path="foo">
<system.webServer>
<httpRedirect enabled="true" destination="/subdir/bar.aspx" httpResponseStatus="Permanent" />
</system.webServer>
</location>
</configuration>

Redirect from page1 to page2, two questions

I have a my-site.ru/page1.apsx which will be a part of my-site.ru/page2.aspx so I need to rewrite (or redirect ?) page1.aspx to page2.aspx
I added this to my web.config:
<system.web>
<urlMappings enabled="true">
<add url="~/page1.aspx" mappedUrl="~/page2.aspx"/>
</urlMappings>
</system.web>
The only thing I don't know hot to fix is: why text in address bar is my-site.ru/page1.aspx but i see my-site.ru/page2.aspx ?? I want while redirecting (rewriting) text in address bar changes too.
Question2: How will it reflect on SEO of page1.aspx ? If possible, please, provide a link. i'm a SEO beginner.
PS: I'm not a native english speaker so there must be some errors.
if you are not going to need the content reflected on page1.aspx or if you are going to move the content to your page2.aspx, I would suggest you to do a 301 redirect, which is a a permanent redirect this will affect your seo passing all the SEO information to the page you´re doing the redirect which is page2.aspx in this case.
from the seo perspective other things you can do are:
- remove page1 from your xml sitemap (if you have any)
- Exclude the page from robots.txt
to perform a redirect either you can do it in IIS 7 (or above) you can include an element in your web.config like:
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/page1.aspx" destination="/page2.aspx" />
</httpRedirect>
or you can code the redirection and check for the url in each request like:
public class RedirectHttpModule : IHttpModule
{
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(this.context_BeginRequest);
}
private void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication application = (HttpApplication)sender;
HttpContext context = application.Context;
//check here context.Request for using request object
if(context.Request.FilePath.Contains("page1.aspx"))
{
context.Response.RedirectPermanent("/page2.aspx");
}
}
}
if you are using .NET 4.0 or above take a look to the permament redirection
just be aware that if you perform a regular response.redirect you are doing a 302 redirect that will affect your seo.
on page load
Write this
server.transfer("Page2.aspx")

How to 301 redirect in ASP.NET 4.0?

I am trying to implement URL redirect for the website rather than doing it page by page. I want to do it in the global.asax file. Below is the code i have defined.
I want to have http://website.net as my main url & want to have a permanent URL redirect if someone types in http://www.website.net.
Unfortunately it is not working for the live website. Can anyone point out the problem in the code. The code doesn't generate any error.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
}
}
Main problem: Your're doing the above stuff in Application_Start - which is only executed once. You should hook up with each request. Try this:
void Application_BeginRequest(object sender, EventArgs e)
{
// Code that runs on every request
if (HttpContext.Current.Request.Url.ToString().ToLower().Contains("http://website.net"))
{
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.AddHeader("Location", Request.Url.ToString().ToLower().Replace("http://website.net", "http://www.website.net"));
}
}
An even better approach would be to use URL rewriting, which can be configured from within Web.Config:
Microsoft rewriting module - Force www on url Or remove www from url
If using IIS 7 or higher, the simplest solution is to use the httpRedirect element in your web.config.
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Permanent">
<add wildcard="/MyOldAspFile.aspx" destination="/MyNewFile.aspx" />
<add wildcard="/MyOldHtmlFile.html" destination="/MyNewFile.aspx" />
</httpRedirect>
This method is very powerful, for example if you have changed the domain but the pages are the same, you have just to add:
<system.webServer>
<httpRedirect enabled="true" childOnly="true" destination="http://www.mynewdomain.com/" />
</system.webServer>
I wrote a small article here: ASP.NET 301 permanent redirects: the best solution
Version 4 of .NET actually has an improved function for single page implementation - the redirectpermanent.
Response.RedirectPermanent(NEW_URL);
Building on previous correct and helpful answers, here are a couple specific examples. Assuming you want to delete the old page (as I did), there are a couple of options.
OPTION 1: Modify the Global.asax
void Application_BeginRequest(object sender, EventArgs e)
{
// Add permanent redirection for retired pages
if (Request.Url.LocalPath.ToLower().StartsWith("/[OLD PAGE NAME]"))
{
Response.RedirectPermanent("/[NEW PAGE NAME]", false);
}
}
OPTION 2: Modify the web.config
<system.webServer>
<httpRedirect enabled="true" httpResponseStatus="Permanent">
<add wildcard="/[OLD PAGE NAME]" destination="/[NEW PAGE NAME]" />
</httpRedirect>
</system.webServer>
if you didn't know what is application domain name ,use something like this
protected void Application_BeginRequest(object sender, EventArgs e)
{
if(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).Contains("localhost"))return;
var leftPartOfUrl = HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Authority).ToLower();
if (leftPartOfUrl.StartsWith("http") && leftPartOfUrl.Split('.').Length == 1)
{
var fullUrl = HttpContext.Current.Request.Url.ToString();
HttpContext.Current.Response.Status = "301 Moved Permanently";
HttpContext.Current.Response.StatusCode = 301;
HttpContext.Current.Response.AddHeader("Location", fullUrl.Insert(fullUrl.IndexOf("://", StringComparison.Ordinal) + 3, "www."));
HttpContext.Current.Response.End();
}
}

Resources