Remove HttpPage Extension in asp.net web application - c#

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

Related

When I try to do routing I get a parse error in ASP.NET Web Forms

I published the web site and uploaded it to FTP (ASP.NET Web Forms C#).
I use routing in the project and this is my routing page's code (I only write 1):
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
public void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("HomePage", "HomePage", "~/Views/Default.aspx");
}
And my folders and pages are as follows:
The code of the Default.aspx page outside the Views folder:
protected void Page_Load(object sender, EventArgs e)
{
Response.Redirect("/Views/");
}
When I try to enter mywebsite.com and enter the site, I get the following error:
But when I try to enter mywebsite.com/HomePage it works. I want to go to the main page when I write the site name directly. Where do I make mistakes?
I forgot to add: It's working on local btw. But doesn't work on FTP.
You have your routing configured incorrectly. The the home page URL is setup as HomePage instead of an empty string (/HomePage).
routes.MapPageRoute("HomePage", "HomePage", "~/Views/Default.aspx");
Change that to (/):
routes.MapPageRoute("HomePage", "", "~/Views/Default.aspx");
Also, delete your "Default.aspx page outside of Views folder". That is going to conflict with your routing and (even worse) it will cause an HTTP 302 redirect to your home page. An HTTP 302 redirect returned from the request tells the user's browser to make a second request to your server, which is bad for both performance and SEO reasons.
Given that your /HomePage URL is working correctly, the above changes should fix your problem.

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 ?

Webform WebAPI routing pages based on Url

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

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
}

inline to handle url redirect

I am migrating a site from siteA.domain.com to siteB.domain.com. All the page paths remain the same. The problem is it's a gradual migration, and not every path is being migrated at the same time. So what I need to do is check the path the user is going to, and if it's a member of a list of migrated sites, then redirect the user from siteA.domain.com/path to siteB.domain.com/path
I was hoping to add in-line c# code to the master page. Any thoughts/examples of this?
I believe the correct way would be to add some routes to the Global.asax.
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
RegisterRoutes(RouteTable.Routes);
}
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("home",
"home",
"~/Home.aspx");
}
The above code will let you type "http://mysite.com/home" and it will redirect to the Home.aspx page. You could redirect to http://myothersite.com/Home.aspx instead of using the ~, or relative path.
You can add routes for each and every page that you have in some master list.
I would have a list of address in your config that have been migrated, then in the page_load of the master page check the current url (on of the properties in Request.Url I can't remember which) and see if it is the list from the config.
Simple, but quite often the simple way is the best. Plus if it is a temporary thing there is no point wasting time doing anything complex.
Any reason not to use IIS for the redirect? SO Question - How to redirect a URL path in IIS?
Create an IHttpHandler that intercepts all incoming requests and redirects appropriately.

Categories

Resources