AttributeRouting generate links with specified language - asp.net-mvc

I would like to know if there is some way to using Html.ActionLink to set in which language has to be generated the URL of link using AttributeRouting. I have tried with:
Html.ActionLink("DescriptionLinkText", "Controller", "Action",
new { language="en" }, null)
but it doesn't work. Is there a way to set to AttributeRouting in which language has to be generated the URL.
What I need is to put at the top of my website links to change the current language. For example if the user is at /en/contact and click on links to change to spanish I want the same page was reloaded but in spanish language /es/contacto instead of redirecting to home page, for this reason I need to generate URL's in different culture of CurrentCulture to allow to change language and continue to the same page.
Thanks for your help

Why don't you use resources for localization?
#Html.ActionLink(LocalizedResources.ActionLinkLabel, "Action", "Controller")
update:
#Html.ActionLink(LocalizedResources.ActionLinkLabel, LocalizedResources.Action, LocalizedResources.Controller)

Related

Creating a multilingual link to a CMS page in Prestashop

I want to make a link to a CMS page in prestashop, the webshop has 3 languages. How do i make the link autoswitch the language?
When you make a cms-page, you can enter the translations for the title of that page. So i have to get that title in the code?
This is what i have now (which stays in English when i switch the language):
<a href='{$link->getCMSLink('8', 'about-us')}' title='{l s='About us'}'>{l s='About us'}</a>
Thanks!
Why don't you simply only pass the id_cms parameter? By providing an alias, you force the dispatcher to use this alias when creating the link.
If you use {$link->getCMSLink('8')} Prestashop will get the current language and find the proper alias in that language.

How to properly encode links to external URL in MVC Razor

This view suppose to show a list of hyperlinks, each pointing to an external URL. The goal is for the user to click one of these links and have their browser open a new tab with the selected URL.
Currently I have the following markup:
#Html.ActionLink("SomeSite", "http://subdomain.mydomain.com/SomeSite")
This markup produces:
http://localhost:58980/AccessInstance/http%3a/subdomain.mydomain.com/SomeSite
instead of :
http://subdomain.mydomain.com/SomeSite
What can I change in my markup to make this work as I expect?
You don't need to use #Html.ActionLink for that. Just use a plain A tag:
SomeSite
Html.ActionLink is specifically for generating links to actions defined in MVC controllers, in the same app. Since you're linking to an absolute URL, you don't need any of the functionality that Html.ActionLink provides.
Two ways :
1. update the database column with full link:
eg SQL:
update ProductTable set ProductLink='http://www.example.com/Product/Mobiles' where ID=123
In asp mvc view
View
2. Hardcode the http part and list from model
View
Hope helps someone.
While a ViewBag is overused and not the best choice most of the time this is something that I had done when inheriting someone else's mvc app to do a quick fix for a URL that I needed to redirect to with a specific dynamically changing querystring parameter
<a target="_parent" href="http://localhost:56332/services/#ViewBag.factory">View Service</a>
Using .NET Core 6
This seems to be the most correct answer:
Link
This will generate the following result:
As you can see at the bottom left corner of the window before clicking the link, the URL address was rendered as it is (NOTE: The cursor was recorded out of place for some reason, that's a ShareX problem, ignore it).
Than link will be directly saved as a nvarchar(750) type (probably any character like type will do the work). No changes to the original link were made before saving it or on reading:
You need to take into account your RouteConfiguration.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}"
because you are specifying the action link as the entire link that you want to redirect.
I would recommend that you use the #rossipedia answer because you can make tricky things like putting a span inside the link
Here to display link that are clickable in index page
<td>
#Html.ActionLink(item.FileName, "../Uploads/Catalogue/"+item.FileName)
</td>

Same link in menu and in footer are in different languages

My ASP.NET MVC 4 website URLs looks like this "/language/controller/action".
HTML code of link in menu and in footer are the same.
<li>Website development</li>
When site is opened in "en-GB" culture link in menu pinting to
/en-GB/Service/WebsiteDevelopment
But link in footer is
/hy-AM/Service/WebsiteDevelopment
How it is possible? How can I fix this?
You don't specify "language" in Url.Action, so it probably takes the default language.
ASP.NET links are absolute ones, so without specifying the language, it's
/default-language/controller/action
If you want the language to be taken from the current URL, you need to write your own Url.Action function, or extend it.
Last menu was changing entire language to Armenian. Aa a route value it takes place after that function.

How do I use an ActionLink with the RouteValueDictionary?

I'm trying to make my URL look like this:
http://domain.com/controller/action/123/SomeTextForSEO
I tried using an Action Link, but that appends ?company=SomeTextForSEO instead of the company name after a slash.
<%: Html.ActionLink("DomainList", "Index", "DomainList", new { id = item.CompanyID, company = item.CompanyDisplayName.Trim() }, new object { })%>
and now I think I need to use a RouteValueDictionary but I'm unsure of how to do this in ASP.NET syntax. Can someone point me in the right direction? The MSDN examples are insufficient.
If you go to global.asax.cs and add a new route similar to the existing default route with the pattern
"{controller}/{action}/{id}/{company}"
Before the default one, you should find that the link will generate correctly with your ActionLink call as-is.
I'm on a phone so I'd like to be more verbose (a route restriction would be a good idea to prevent this route interfering with the default), but the HTC keyboard is not code friendly ;)

Html.Action link and Html.RouteLink

Even If the url is same can i go to different action using Html.RouteLink and Action Link.Like when i click on news link i will go to news details.The url of this page is http://localhost:1390/en-US/latestnews/125.Now if i select the ddl of language in the site header in this pagei need to
go to the home page of the site.The ddl (on change) will take the same url but this time it needs to go to action in the sane controller.
The URL will direct to the controller/action on the first routing entry that it matches regardless of how you generate it. I'm not sure exactly what you are trying to accomplish, but I suspect that what you need to do is use javascript to direct to a different url, perhaps generated with Html.RouteLink instead of Html.ActionLink based on the value of the drop down list when it's selected. If I'm misunderstanding what you are trying to do, please clarify.

Resources