Webform WebAPI routing pages based on Url - c#

I would like to reroute my url e.g.
http://localhost:1756/homepage.aspx
to
http://localhost:1756/pages.aspx
in pages.aspx then I process the original url to see if it's homepage.aspx, products.aspx etc.
To load the right content.
I am using
RouteTable.Routes.MapPageRoute("pages", "{page}", "~/pages.aspx");
in global.asax
Is this the right way to do? Or is there a more elegant way?

in your global .aspx you need to register you routes like below:
//to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller
RouteTable.Routes.Ignore("{resource}.axd/{*pathInfo}");
//if you don't need parameters on your url then it would like this:
RouteTable.Routes.MapPageRoute("Pages", "pages", "~/pages.aspx");
//if you need a parameter on your url then it would be like this:
RouteTable.Routes.MapPageRoute("Pages", "pages/{id}", "~/pages.aspx");
another way would be having all of your routing urls in a class and put them on App_Start folder then register it from your global.asax file like this.
//global.asax
protected void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
//App_Start/RouteConfig.cs
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.Ignore("{resource}.axd/{*pathInfo}");
routes.MapPageRoute("Pages", "pages", "~/pages.aspx");
}
}
you could find more info about routing here

Related

ASP.Net Friendly URL 404 error in Visual Studio 2015

So I have an ASP.Net web forms project.
Global.asax.cs contains:
void Application_Start(object sender, EventArgs e)
{
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
And RouteConfig.cs has:
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
System.Diagnostics.Debug.WriteLine("Got inside registerroutes.");
}
Seems like those are the things I need for friendly url's to work. And any address I visit, like "localhost/Default.aspx", gets turned into a friendly URL, "localhost/Default" in this case. So that part seems to be working at least. I also see my little debug statement get spit out, so the RegisterRoutes method is for sure running.
But any friendly style URL (localhost/Default lets say) I try and visit or get redirected to gives me a 404 error. Can anyone tell me what I'm missing?

The page isn't redirecting properly searched a lot but cant find solution

im trying a simple routing and i have my global.asax as below
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
registerroutes(RouteTable.Routes);
}
public static void registerroutes(RouteCollection routecollection)
{
routecollection.MapPageRoute("home","home/","~/home.aspx");
}
my home page
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Response.Redirect(ResolveUrl("~/home/"));
}
}
but not not redirecting properly dont know where im wrong
Are you using the Microsoft Friendly URL Nuget package? (If not, I'd highly suggest it)
If you use it you can avoid using the redirect in your homepage -
you need to create a RouteConfig.CS in App_Code>App_Start the add then following code (make sure you are using System.Web.Routing, and Microsoft.AspNet.FriendlyUrls
public static void RegisterRoutes(RouteCollection routes)
{
var settings = new FriendlyUrlSettings();
settings.AutoRedirectMode = RedirectMode.Permanent;
routes.EnableFriendlyUrls(settings);
routes.MapPageRoute("", "", "~/default.aspx");
}
in the global.asax you then add
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
}
This should then work without you needing any sort of redirect in the homepage.
Please remove the ResolveUrl as it's not intended to be used here, it's only for resolving the scripts and resource files in the view / aspx code.
Response.Redirect("~/home");
One more thing, pay attention to C# coding standards , function names should be pascal case:
RegisterRoutes instead of registerroutes
Also, make sure you have a physical file for the home.aspx page that is placed on the root of your website.
If it's still not wroking, we need to know what kind of error you are getting and how you are running your app ? using the IIS express ? or IIS server ? or the classic development server ?

ASP.NET Hide Home Page Extension

I have used URL rewriting in my ASP.NET 4.0 project to achieve the URL routing. Here is a sample code from my Global.asax:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
private void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("main", "Main", "~/Home.aspx");
routes.MapPageRoute("aboutUs", "About-Us", "~/AboutUs.aspx");
...//so on and so forth
}
I also added this to Application_BeginRequest to forward Home.aspx to Main in the same Global.asax:
protected void Application_BeginRequest(object sender, EventArgs e)
{
if (HttpContext.Current.Request.Url.AbsolutePath.Contains("Home.aspx"))
{
HttpContext.Current.Response.Redirect("~/Main");
}
}
This way, any request that tries to go to "Home.aspx" will be routed to "Main", which happens only in the first load. The routing works for all pages in my project.
Now, when I publish my code to my website and launch the website, this is what the URL comes up:
www.MyWebSite.com/Main
How can I hide "Main" at the end of the URL and make it look like this:
www.MyWebSite.com
~ Thanks
Configure Home.aspx as "default page" in the Web Server.

ASP.NET Routing webform query

I have a ASP.net Web forms project that I am working on.
I need to have a add club page where the admin can add a club to the website.
When the club is added it creates a small website within my website for that club automatically. I think this uses some form of string builder.
Then that club should be able to store their own details on that page.
I was going to do this doing asp.net routing. Would this be the correct way of going about this?
Having looked at different examples i would need to have the url for the webpage automatically generated
assuming you are using asp.net 4.0.. something like this
Global.asax.cs
protected void Application_Start(Object sender, EventArgs e) {
RegisterRoutes(RouteTable.Routes);
}
protected virtual void RegisterRoutes(RouteCollection routes) {
routes.MapPageRoute("clubs"
, "clubs/listing//{clubNumber}"
, "~/Clubs/ManageClub.aspx");
}
so urls like /Clubs/Listing/ClubNum101 would resolve to /Clubs/ManageClub.aspx
In ManageClub.aspx check for RouteData e.g.
if (Page.RouteData != null && Page.RouteData.Values.ContainsKey("clubNumber")) {
//do something here
}

Remove HttpPage Extension in asp.net web application

I want to remove Http Page Extension like this
my actual page:
http://test.com/dashboard.aspx
i need to modify as follows
http://test.com/
for all redirection of .aspx page.
P.s:
i dont want to use URL rewriting.
Use asp.net 4's routing engine. You can specify a routing rule in asp.net 4 as a default route.
Check out:
http://www.xdevsoftware.com/blog/post/Default-Route-in-ASPNET-4-URL-Routing.aspx
for a very basic one that may work in your scenario try this in your global.asax.cs to map everything to say default.aspx
void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("Default", "{*whatever}", "~/default.aspx");
}
You can write HttpModule where you can scan incoming url and make all that you want before request will be processed.
http://msdn.microsoft.com/en-us/library/ms227673.aspx

Categories

Resources