How to get extension less and without query string SEO friendly URL for asp.net web forms.?
I have found out a very good article Here
It is a very good blog post written on how to redirect urls which contain query strings as extension less seo friendly urls.
One method of doing it by including Global.asax into the application.
Here is the example.
Include Global.asax into the application.
<%# Import Namespace="System.Web.Routing" %>
inside global.asax file
void registerroute(RouteCollection routes)
{
routes.MapPageRoute(
"Home-Route",
"Home",
"~/Default.aspx"
);
}
Which will map the home page or default page
For Query string urls like http://xyz.com/page.aspx?id=about
routes.MapPageRoute(
"Page-Route",
"Pages/{page}",
"~/page.aspx"
);
Then call this registerroute() inside application start event under Global.asax
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroute(RouteTable.Routes);
}
Then to access the query string inside pages.
string pg = Page.RouteData.Values["page"] as string;
Related
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"));
Need to setup a URL for data to be sent, as seen below.
http://yourdomain/getdelivery.aspx?batchid=$batchid&mobile=$mobile&status=$status
Hello,
I googled but almost all the results were how to get the data and not how to set up the web page and set the domain. Me being a newbie both as a sysadmin and certainly with web development I got stuck. I ask for your help to set me in the correct course.
Best Regards,
The simpliest way is to add a web form page called GetDelivery.aspx.
In GetDelivery.aspx :
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="GetDelivery.aspx.cs" Inherits="PortalBmoTrsb.GetDelivery" %>
In GetDelivery.aspx.cs :
namespace MyNameSpace
{
public partial class GetDelivery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
var batchid = Request["batchid"];
var mobile = Request["mobile"];
var status = Request["status"];
}
}
}
You also should check a look to Web Api Controller.
I have an ASP.NET 4.5 Web Forms Application utilizing Bootstrap. http://goo.gl/GZZp9r
I had a problem whereby Site.Mobile.Master was being utilized whenever the website was being rendered in Extra Small ViewPort. Since using Bootstrap I did not need this Site.Mobile.Master.
I had implemented a solution that would cause Site.Mobile.Master to not render, instead only utilizing Site.Master.
public partial class Site_Mobile : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
var AlternateView = "Desktop";
var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
Response.Redirect(url);
}
}
The above solution is causing problem with GoogleBot because of 302 Redirects.
Someone has indicated that:
"when the user agent is Google mobile when your homepage is requested your site responds with a 302 redirect to
/__FriendlyUrls_SwitchView?ReturnUrl=/
and then the request is 302 redirected again to /
I have read that I cannot simply delete Site.Mobile.Master.
Is there a better solution to NOT render this Site.Mobile.Master for Extra Small ViewPort?
I'm not sure what the benefit would be to use this solution.
But hopefully this would resolve 302 Redirects that is bad for GoogleBot.
<browserCaps>
<result type="System.Web.Mobile.MobileCapabilities, System.Web.Mobile, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
<filter>isMobileDevice=false</filter>
</browserCaps>
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....
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 .