how to solve mistake in url rewriting? - asp.net

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
}

Related

Route from aspx code behind to MCV controller

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"));

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 ignore some route while using ASP.NET Friendly URLs?

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....

Custom Url Rewriting in 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 .

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

Resources