Sitecore Custom Routing (Not MVC) - asp.net

I followed this post and this in order to test create a custom routing entry for an item. I tried to register a route in global.asax something like this...
void RegisterRoutes(RouteCollection routes)
{
routes.RouteExistingFiles = true;
routes.MapPageRoute("about", "about/us", "~/About/Company", false);
}
but the unfortunate thing is, this results in 404. I think the asp.net routing is looking for a physical page "About/Company.aspx" where as it is more of a rendered page/link out of sitecore/content/site/about/company in the sitecore tree.
Do you know if there is way to redirect it to a rendered sitecore url/item?

Using Routing to do this is possibly not the most optimal solution.
The easiest way of mapping URLs to a specific item is by setting up Aliases. See chapter 6.1 in the Data Definition Cookbook for more information about Aliases.
If you need more control you could create a custom ItemResolver that can sit after the default ItemResolver in the HttpRequestBegin pipeline and can run your custom URL/item mapping.

Related

Mapping RouteTable on PreInit() - Will it cause any problems?

My page route mapping changes depending on certain criteria. To determine this criteria I need access to HttpRequest which means I can't do my route mapping in Application_Start(). On that note I have done it on PreInit() on my default page and it seems to work without an issue. However, all the examples I have seen with Route Tables are doing it in Application_Start, is this purely to avoid clearing the route list and adding them again? Will there it cause any harm to my web application doing in the way I am doing it?
EDIT: Example:
I load controls from different folders based on the project number and whether the website is being viewed on a mobile device, on that note I need to know this information before mapping my routes, like so:
RouteCollection.MapPageRoute("OneParam", "{Action}.html", String.Format("~/{1}{2}/Default.aspx", ProjectNumber, MobilePathStr));
which would map to something like ~/1234/Mobile/Default.aspx or could map to ~/1234/Default.aspx.
Don't do that.
Instead, you should create your own RouteBase class which looks up that information for each request and runs the appropriate handler.
To send to an ASPX page, you can either return BuildManager.CreateInstanceFromVirtualPath(virtualPath, typeof(Page)) as Page; or create a PageRouteHandler and delegate to it.

Can an ASPX page be given an alias so that it can be accessed from two different URLs?

I need to access the same page via two different names for the page.
Example:
CustomersDetail.aspx needs to be accessable using the aliased name PartnersDetail.aspx
CustomersDetail.aspx is the real file.
Both of the following urls should map to the same page:
http://www.example.com/CustomersDetail.aspx
http://www.example.com/PartnersDetail.aspx
Is this possible using the Web.Config? If this is possible can the page know which url it was accessed from by looking that the request uri?
4GuysFromRolla does an excellent job of explaining ASP.NET 2.0's method of url mapping within the web.config which allows for very readable and easily maintainable url mapping.
Essentially you will want to put the following in your web.config inside of the system.web section:
<urlMappings enabled="true">
<add url="~/PartnersDetail.aspx" mappedUrl="~/CustomersDetail.aspx" />
</urlMappings>
Depeding on the version of the .Net Framework (introduced in 3.5) you are using you could add an entry to the RouteTable.Routes collection in the Global.asax on application start:
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
void RegisterRoutes(RouteCollection routes)
{
routes.MapPageRoute("DetailReRoute",
"PartnersDetail.aspx", //Virtual Page
"~/CustomersDetail.aspx", //Physical Page
false, null, null);
}
May be it is a bit tough, but if you won't find a clean solution and do not want to go deep into URL-rewriting why not create the second page and just put Response.Redirect (page needed - CustomersDetail.aspx - and its URL will be shown) or Server.Transfer (page needed and the URL http://www.example.com/PartnersDetail.aspx will be shown) into the Page_Load?
You could also put UserControls on each page.
I meant that you could create both pages, with the names you want and place the actual content inside an UserControl. After that, you put the UserControl in both pages. Whenever you want to change the content you just change in the UserControl and it will be replicated.
I know this is not the best solution, but works nice if you want aspx pages immediately.

How to URL route based on UICulture settings?

I have ASP.NET 4 project (not MVC). I need to create url route based on user input language.
Project has only two languages "he" and "en".
User can enter the site and if his culture is set to anything besides he-IL i want to re-route him to website.com/en/ otherwise to website.com/he/
Default.aspx should remain same page which uses Globalization features translate values based on user's culture settings in browser.
How can i do that? what should i do besides writing a route in Global.asax and How to write this route.
This shouldnt be hard. Yes the Global.ascx is the best place to start.
First map the Routes,
protected void RegisterRoutes(RouteCollection routes)
{
//Contact route for EN culture
routes.MapPageRoute(
"contactRouteEN",
"en/contact",
"~/Contact.aspx"
);
routes.MapPageRoute(
"contactRouteHE",
"he/contact",
"~/Contact.aspx"
);
}
protected void Application_Start(object sender, EventArgs e)
{
RegisterRoutes(RouteTable.Routes);
}
That much establishes the routes.
The problem your describing sounds more like a Globalization issue than url routing problem. The url portion of the issue will be cosmetic to the user but won't attack the underlying issue in my view. ASP.Net provides facilities for Globalization. For example you can use LocalResources. To do this for the pages at your applications root level (not nested inside folders)
Right click the website and choose Add ASP.Net Folder
Choose App_LocalResources.
Right click the App_LocalResources folder and choose Add Item
Choose Resource File.
It is important that you name the file according to the culture you plan to target
You can create the first file to be Contact.aspx.resx to be the default resource file (maybe english?)
ASP.Net will try to find the most specific culture to match the resource files to and will resort to the default if a more specific is not provided.
The naming convention follows PageName.aspx.languageID-cultureId.resx
You could have Contact.aspx.he.resx
In a label control for example you could set it like this
<asp:Label ID="lbContactMessage" runat="server" Text="something" meta:resourcekey="yourmatchingkeyfromresourcefile"></asp:Label>
For more info see
http://msdn.microsoft.com/en-us/library/c6zyy3s9.aspx
Seems like you are trying to do something unintended with Routing.
If the language in URL does nothing and you need for it to appear in URL only then you either make hackish solution with HTTP modules rewriting urls to remove it and add back to generated html or simply map the same site to two virtual folders /en and /he in IIS and make a simple Default.aspx page at / to redirect to appropriate one based on user culture.

asp.net MVC giving a controller the name 'AdminController' makes it not work

I am working on a ASP.NET application that uses ASP.NET MVC.
I tried naming one of my controllers "AdminController" meaning I typed "Admin" in the new controller text box and it filled out the controller part all by itself of course.
This controller never worked until I changed it's name. If I changed the name to anything else it worked with no problems.
I looked inside my Global.asax.cs file where the routes were configured and I found no routes leading to it.
I tryed adding a route to this new controller like this:
routes.MapRoute("Admin", "calcul/SomeAction",
new { controller = "Admin", action = "SomeAction" });
and it worked but then mysite\admin would only get routed to that specific action.
I renamed the controller to AdminSection and it works but I don't understand why it didn't work before.
Does anyone have any idea
You shouldn't need to create an explicit route for your controller if the default route matches it (controller/action/id).
Also, in the new controller text box you would need to type AdminController rather than just Admin.
The MVC framework will look for classes that end with this when looking for possible controllers.
It was me, there was a area named admin that I didn't see. When I excluded it from the project the "adminController" controller started to work

ASP.Net Routing with WebForms

I am trying to cut over an existing WebForms app to use Routing, and want to do it in phases. However, I am running into some issues with a particular route:
//I want to catch existing calls to .aspx pages, but force them through
// a route, so I can eventually drop the .aspx extension
new Route("{page}.aspx", new MyCustomRoute());
This isn't working at all, as calls to [SomePage].aspx are never tripping this route... If I change the route to look like this:
//Same thing sans .aspx extension
new Route("{page}", new MyCustomRoute());
All calls to [SomePage] are getting picked up. Any ideas?
Ok, so now I feel stupid...
Turns out there is this little property on the RouteCollection class called RouteExistingFiles which is false by default. Apparently ASP.Net routing gives precedence to existing files before turning them over to routing, so any calls to existing pages would obviously not be handled by my routes. Setting this property to true resolves my issue, all though it may have unintended side effects to which I am as of yet unaware.

Resources