Globalization of controller names and actions - asp.net

Is it possible to create "aliases" of my controllers and actions, instead of adding one more item to the URL to indicate the language?
Example:
For English:
/controllerNameInEnglish/actionNameInEnglish
For Portuguese:
/nomeControladorEmPortugues/nomeAcaoEmPortugues
The Portuguese version must call the same action as the URL for English.

Please ignore this question. I just found the answer on http://blog.maartenballiauw.be/post/2010/01/26/Translating-routes-(ASPNET-MVC-and-Webforms).aspx

You can also check out a plugin called AttributeRouting that can help you archieve this. The great thing about it is that it's going to be included by default in future versions of ASP.NET MVC.

Related

Acumatica: How to get current localization

I have another site will be called from acumatica. I want when i call a site, i will pass current localization via url.
As title, i want to get current localization after login.
Please help me.
Thanks so much
I have found a answer, i viewed a login.aspx.cs and i saw the cookie store culture.
Cookies["Locale"]["Culture"]
You may also want to check
PXLocalesProvider.GetCurrentLocale()
as one must avoid bypassing the framework.

Best way for ASP.NET MVC5 localization where locale is passed in querystring with attribute routing

A very long title, sorry for that.
I'm looking for your input on the best way to support localization in an ASP.NET MVC 5 project in which i would like to pass the locale as part of the querystring. This way it would be easy for users to share a link to website and also pass the correct locale as part of the link.
If this would be done using, for example cookies, the user would pass a link but the page might be in a different language for the person who receives the link. I don't think that that is very nice.
The solution i currently use (http://afana.me/post/aspnet-mvc-internationalization.aspx) does not work nice with incorrect urls and just keeps loading the same page in loop. There is a lot of information on the web and i already went through a lot but i would like to know what is most commonly used and really works well.
As a bonus i would like the solution to work with attribute routing as my current solution doens't play nice :(
you can use resource files for that, the current culture is in the current thread of the web request, so no need to pass it in the querystring but it can be done;
have a look at this live demo: http://prodinner.aspnetawesome.com
you can download/read pdf about it here: http://prodinner.codeplex.com
you can see there that you can change the language using the dropdown;
in Global.asax.cs Application_BeginRequest, there's already code in there related to language, it reads a cookie now (or it's absence), you can make it read the querystring

A utility to construct correct Url from the controller and action name?

Do we have something like #url.action and #url.content in Spring MVC like ASP.net MVC? If not, I will need to create the urls to the actions myself, which is kind of messy. I feel that something like #url.action and #url.content is very nice to have.
Thanks ...
The correct question to ask should be:
Do we have a utility to contruct the correct Url by giving the controller and action name?
Sorry for the confusion, this question has been asked before. Finally found it.
How can I create a URL based on controller and action method in Spring MVC?
The answer is - no, Spring MVC does not have this support! Please go to the link to vote to add this, since majority of the MVC frameworks do have this function.
Got the answer - Please view another post.
How can I create a URL based on controller and action method in Spring MVC?
Also .. please vote to add this feature.

How to redirect to a page from a non controller in ASP.NET MVC?

I have a non controller file called MyLib.cs in this I have a method that on given condition I want it to redirect to a page.
I used: "RedirectToAction()" but I got an error saying it dose not exist in current context.
Any ideas what I should use?
Thanks!
I think you can use Response.Redirect here.
Consider this code
HttpContext.Current.Response.Redirect("YOUR_PAGE_Virtual_PATH");
// example: http://www.mywebsite.com/home/list
Give it a try and let us know if it worked.
P.S: don't forget to mark as answer if this answer really helped you, thanks.
This is against some of the MVC tenets, you're breaking the Separation of Concerns concept, only the controllers should return views, not your libs.
I can give you two suggestions:
In your lib return a Enum, and
them the controller would figure out
which view should be displayed.
If not all paths from your lib
causes a redirection, throw an exception and the controller handles it redirecting to the appropriate view.
I assume MyLib is called from within a controller?
Could you set variable in MyLib.cs and redirect from your controller based on that once MyLib has finished?
Update :
Do you absolutely have to redirect from this class? I say this because this is going against the MVC paradigm. You should really be handling all your routing within your controllers. Sticking to it really will make your application far more maintainable. Really can't stress that enough! :)

Can you determine the name of the route followed from your webforms page?

I have a ASP.NET 3.5 webforms project I have enabled routing over.
I also have in the project a number of controls doing different things based on what page they are currently being shown in. It would seem that the most straightforward way to control this different behavior is to discover which route was used to load the page and then do things according to that.
However, I can't seem to find a way to discover the route bar looking at the actual request URL and running a regex over it which isn't great. Does anyone know a way to look it up some other way?
Update: there still doesn't appear to be a way to do this in ASP.NET 4.0. Hopefully someone else has figured this out?
It looks like Phil Haack has answered this question in a blog post on his site: http://haacked.com/archive/2010/11/28/getting-the-route-name-for-a-route.aspx
In a .NET 4 webforms app, I used this to determine the route definition.
string myOperation =
((System.Web.Routing.Route)(Page.RouteData.Route)).Url;
//string has value "Stop" or "Start"
Let's say your routes are like so:
routes.MapPageRoute("StopEmailAlerts",
"Stop/{SomeToken}",
"~/Emailing.aspx", false);
routes.MapPageRoute("SendEmailAlerts",
"Start/{SomeToken}",
"~/Emailing.aspx", false);
I posted a couple of simple extension methods you can use to get/set the route name on this post. Seems simpler (to me) than Haack's solution.

Resources