How to URL route based on UICulture settings? - asp.net

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.

Related

Default page not rendering with Site.Master content

I have a hosting account with GoDaddy.com, IIS 7 server running .NET 4.0, and I am in the early stages of developing a web site for our church. The content is a free CSS based template I have moved into an ASP.NET Web App with Master pages. (If critique on content is necessary please keep in mind this is a very early stage of development...but I am open to any suggestions. :) )
For some reason, when I enter the full URL to the default page, the page renders properly. However, if I only enter the folder name without the page name, I only get the content form the page itself.
See for your self:
http://www.websmithsllc.com/lpacftp/Home.aspx
http://www.websmithsllc.com/lpacftp
I don't think this is an issue with my wire-up between the content page and the masterpage as it will properly render when I use the full URL. Therefore, I assume the issue is in one of three areas:
How I am publishing: One Click to an FTP directory
The project settings: Currently Home.aspx is the start page
An issue with the settings on my host.
I really hope the issue isn't #3 because my experience so far has been that their tech support is severely lacking in the area of Visual Studio / IIS development and publishing.
Now, some additional clues. I KNOW that the Site.Master file is being rendered, at least to some extent. The menu that is being displayed is created in the Site.Master.Page_Load event handler:
protected void Page_Load(object sender, EventArgs e)
{
//Load sidebar content
Page p = HttpContext.Current.CurrentHandler as Page;
menuContent.Text = Helpers.StaticHelperMethods.GenerateMenuContent(p.Title);
}
Static method:
public static string GenerateMenuContent(String pageTitle)
{
StringBuilder menu = new StringBuilder();
if (pageTitle == "Home")
{
menu.Append("Home\n");
}
else
{
menu.Append("Home\n");
}
More similar code...
In this case, p.Title should == "Home", but the code is responding as though is does not, and I don't know how (if I can) debug live to see what's going on. Finally, if you look closely at the second link, you'll notice some stock ASP.NET advertising text- that appears to be coming from the stock "Default.aspx" file in the BodyContent asp:Content object. However, looking at the properties/Web tab I can see that the startup action is Specific Page : Home.aspx.
So- hopefully I haven't added a ton of unnecessary info here, but at least enough for someone with more experience to help me figure out what I'm doing wrong here.
Thanks in advance for whatever help you can offer me on this.
You need a default page. Create it and then in the code behind in Page_Load write:
Response.Redirect("Home.aspx");
Or change your default page in IIS. Or change your home page to Default.aspx (and rename the class and the page directive).
Of the three, creating a Default.aspx page that redirects to Home.aspx is likely the easiest.
Yes, MatthewMartin is correct. Your hosting service's IIS is not configured to pick up "Home.aspx" as a default page. You will need to either get them to add it to the IIS configuration, or rename your home page to Default.aspx, or create a "dummy" Default.aspx that redirects to your Home.aspx.

Sitecore Custom Routing (Not MVC)

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.

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 achieve multilingual support in ASP.NET

I want to internationalize my asp.net application. How to do this? What steps exactly do I have to follow?
1.) If you use database, then you must modify your tables. At least with adding the LCID column.
2.) Set default culture and UI culture in web.config
<system.web>
<globalization culture="cs-CZ" uiCulture="cs-CZ"/>
</system.web>
3.) Then you can set actual thread culture either in global.asax in e.g. BeginRequest event, or in base class of your page classes in InitializeCulture method
protected override void InitializeCulture()
{
string language = Request["lang"];
if (!string.IsNullOrEmpty(language))
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(language);
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Threading.Thread.CurrentThread.CurrentCulture;
}
else
{
base.InitializeCulture();
}
}
For static texts you can use Resources. E.g. you create Mytexts.resx where you write texts for default laguage (en-us) and then you create Mytexts.en-UK.resx for uk english and overwrite text that are different from default laguage. Then you can insert this strings in your page :
<asp:Label runat="server" Text='<%$ Resources: Mytests,WelcomeMessage %>' />
This are only briefly steps for beginning with localization, but for small pages / apllications is it sufficient.
Simply make a basepage class that will inherited from Page class, put this method in basepage class and inherit basepage class in your every aspx.cs page to acheive globalization.
protected override void InitializeCulture()
{
Thread.CurrentThread.CurrentUICulture = new CultureInfo("en-US");
Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
base.InitializeCulture();
}
set in this method whatever culuture you want, like ar-sa for arabic....
You have to explore the topics of using resource files in your web application. If you need database based localization support you may try the excellent free product from westwind
Localization and Globalization topics in MSDN are your best bet for this.
It's a pretty big question to be able to give you the exact steps, and there are several different approaches.
The approach we took on my most recent project (simplified) was:
Set up a domain for each country
Create a resource file for all the
hard-coded strings (form labels etc)
for each culture (en-US, de-DE,
fr-FR)
Change the Thread.CurrentCulture based on the domain the site is
being accessed from - this means
that all your number formats, date
formats will be correct and use the
correct localised resource file
Hope this helps!
See here for the Microsoft white papers on Internationalization.
This is a complex topic and requires a lot of work to get right, through all layers of your system.
Start here.
ASP.NET resx files which will let you configure constant strings easily, but your DB will also need to support unicode, and you'll need to do different things depending on the languages you wish to support.
Good luck, and ask questions when you have specific problems.
See this link on creating localized resource files: http://msdn.microsoft.com/en-us/library/ms247246.aspx
Basically you create a new resource file each language/culture you want to support. Then you access the strings inside them by name in your markup pages and code behind files.
Additionally these resource files need to be in a specific folder in your project called: App_GlobalResources
Global resource files must be in the App_GlobalResources folder. If you
try to create a .resx file outside of
this folder, Visual Web Developer
prompts you to create it in the
folder.

Passing value through querstring without showing the destination page in the URL with ASP.NET web form

I have a small web app being written in ASP.NET, VB.NET , .NET 3.5
I pass a value from default.aspx to demo.aspx using query string. When Go button is clicked the URL will be like this : localhost/demo.aspx?id=4
But I want URL to be sth like this when Go button is clicked : localhost/?id=4 and the id value is passed to demo.aspx so the expected result can be shown. How to get this done if possible without using the routing technique.
This will work only if the web server considers demo.aspx to be the default document. When you load a page without stating the page name (such as you localhost/?id=4 example), the web server will locate a file with a pre-defined name and use it as the default document. Often this file is called default.aspx (or .asp, .htm, .html, .php or something like that). So if you want to load any other page you will need to either state the name, or use URL rewriting techniques.
Modify your go button to link button and set herf = "?id=[id]"
Modify Page_Load event of Default.aspx by using the following code.
public Page_Load(object sender,EventArg e)
{
if(!string.IsNullOrEmpty(Request["id"]))
{
// Using Server.Transfer() function to call Demo.aspx page.
// Client still see "localhost/?id=[id]" at address bar.
Server.Transfer("Demo.aspx?id=" + Request["id"]);
}
}
Something like this?
protected void btnGo_Click (Object sender, EventArgs e)
{
Response.Redirect("localhost/?" + Request.QueryString);
}
Not sure when you want the url to change? Not sure what the routing technique is either. Sorry.
if you want it to work with postbacks, you still need to make sure the webserver is configured with a default document like default.aspx.
in 3.5 sp1 MS finally allows us to set the action attribute of the form.
so on page init you can explicitly set it now
Form1.Action = "/mydirectory/"
Caveat: Some versions of IIS do not allow posting to a directory. (XP IIS for example)

Resources