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
}
Related
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.
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 ?
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.
I am working in as asp.net application which is in asp.net 3.5 version. I have a requirement to implement URL rewriting. I have defined 4 pages like
www.abc.com/page1.aspx
www.abc.com/page2.aspx
www.abc.com/page3.aspx
www.abc.com/page4.aspx
I want that when user types www.abc.com/language1 then
www.abc.com/page1.aspx open. If user types www.abc.com/language2, then www.abc.com/page2.aspx should open.
Please suggest a solution to it.
Also, as this site is complete and have links sent through email to users ( and some of the links have querystrings) what is best way to redirect users to new urls without querystrings and generate new links using new pattern ?
I have gone through followig techniques:
http://www.iis.net/downloads/microsoft/url-rewrite ( this is with IIS, can i use it with asp.net 3.5 and without IIS ? )
ASP.NET URL Rewriting in very easy way ( it is not tested for security issues)
http://www.hanselman.com/blog/IntroducingASPNETFriendlyUrlsCleanerURLsEasierRoutingAndMobileViewsForASPNETWebForms.aspx ( This require existing links to change ? )
Please suggest
public class Global : System.Web.HttpApplication
{
void RegisterRoutes(RouteCollection routes)
{
routes.Add(new Route("language2", new PageRouteHandler("~/page2.aspx")));
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
}
You can do this under Application_BeginRequest in Global.asax file, Check If Request comes from specified URL, then Redirect user to another Page, eg : if(Request.Url.ToString().ToLower().Contains("language1.aspx") , then Response.Redirect("Page1.aspx") .
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