Custom Url Rewriting in asp.net - asp.net

can anyone please provide me the details how we can implement custom URL rewriting in asp.net
My current url is look like below :
www.domainname.com/News/default.aspx?newstitle=todays latest news
And now I would like to redirect to below url :
www.domainname.com/News/todays-latest-news
Please suggest me how we can achieve the same.

Add this to global.asax
using System.Web.Routing; //top of the page
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("", "news/{news}", "~/news/default.aspx");
}
And then you can get the news title in default.aspx like below:
protected void Page_Load(object sender, EventArgs e)
{
if (this.RouteData.Values.Count > 0)
{
string newstitle = this.RouteData.Values[0].ToString();
}
}

to achieve your task use the concept called asp.net routing ,
here is the few examples refer for better understanding
http://www.codeproject.com/Articles/77199/URL-Routing-with-ASP-NET-4-0
http://msdn.microsoft.com/en-us/library/cc668201%28v=vs.100%29.aspx

You can use URLRewriter.Net for this purpose. It's very easy to integrate into asp.net project and also it's open source . Add the dll file of urlRewriter.Net into your project and set the rewriting rule in your web.config file. Although be careful when using it with ajax postback pages. In Raw url if you get ajax postback problem .

Related

ASP.NET code source updating

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>

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
}

How can I redirect url on my my host?

I want to redirect url to my site.
I mean I have this domain.
www.samplesite.com
and I have this site : www.samplesite.com/test/index.aspx
but I want to enter www.samplesite.com and goes to my site.
How can I do that?
I use dot net ,4.5 framework.
When using asp.net web forms you can create a Default.aspx page, in the Page_Load method in the Default.aspx.cs file call this:
private void Page_Load(object sender, EventArgs e)
{
Response.Redirect("~/test/index.html");
}

Asp.net page routing not working

I am trying to add page routing (I use regular asp.net 4.0, not mvc), so that when a user goes to:
http://sitename.com/public/member/view/andrey
they would get to:
http://sitename.com/public/memberprofile.aspx?userName=andrey
I added following in Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/View/Member/{username}",
"~/Public/MemberProfile.aspx");
}
But when I try going to http://sitename.com/public/member/view/andrey in my browser, I get 404
Is there anything else that needs to be done for this routing to work other than adding a page route map?
Thanks!
Your route says Public/View/Member/{username}
But your link is /public/member/view/andrey
This would definitely 404
Why not try and change your route to
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.MapPageRoute("MemberViewRoute",
"Public/Member/View/{username}",
"~/Public/MemberProfile.aspx");
}
and see what happens
I actually found this great article that helped me fix my problem: http://blogs.msdn.com/b/rjacobs/archive/2010/06/30/system-web-routing-routetable-not-working-with-iis.aspx

When and where does a UserControl compile

I have a page, Default.aspx, and a UserControl, HelloControl.ascx. In the page, I dynamically instantiate the control as follows:
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = Page.LoadControl(typeof (HelloControl), null) as HelloControl;
c.Greet();
}
This works fine, and the user control writes "Hello from a control" to the response. I have no #Register directive in Default.aspx, but when I try a similar dynamic control creation on a client's machine, I get an error that the "Type or Namespace does not exist".
I have even gotten feedback, on forums, from MS, that I need the #Register directive, but I obviously don't. Can anyone help me out with info on how and when the user control is compiled if no #Register directive references it?
EDIT: I have tried a different direction of investigation, losing my initial call to LoadControl, and I still can't reproduce the problem. The following code also works fine on my dev machine, without any #Register directive.
protected void Page_Load(object sender, EventArgs e)
{
HelloControl c = new HelloControl();
Response.Write(c.Greet());
}
use full path to control :
this.Controls.Add(Page.LoadControl("~/CustomControls/HelloControl.ascx"));

Resources