is it possible to generate cross-application action links using the HTML helper class?
I have the following (both are separate VS2008 projects):
http://mainwebsite/
http://mainwebsite/application
I would like to generate a link IN the /mainwebsite/application/ project TO /mainwebsite/. Is this possible? Or should I just hardcode a hyperlink?
addition:
My question also applies vice-versa. So generating a link IN /mainwebsite/ TO /mainwebsite/application/. I already managed to do like so, by simulating the application name as controller name:
<% =Html.ActionLink("ApplicationName","",new With {.Controller = "application" }) %>
Yes, you can do this. The HTML helper generates URLs using the routing system, which is completely ignorant of ASP.NET MVC and your application. If you have a route in your route table for the other application, then you can generate a URL for it using the HTML helper. There is no rule which states that your route table can only contain routes for the current application, although you obviously need to be careful about how you order the routes.
Related
Migrating our classic ASP site to ASP.Net 4.5 MVC. Our site is pretty large, hundreds of file, and SEO optimized. All URLs are SEO friendly, via web.config, eg.:
www.ourdomain.com/articles/2013/120/the-best-article
www.ourdomain.com/blog/2013/122/the-best-blog
www.ourdomain.com/video/2013/123/the-best-video
ie. www.ourdomain.com/{contenttype}/{year}/{id}/{url encoded title}
The ASP files to render are stored in a folder like /render/content.asp
We don't want to change any URLs in this migration. The new files of the MVC app will be in all new directories than the existing site. For example, a view: /MVC/content.vbhtml
How would you go about adding links in your Views, with ActionLink or other, to use the existing SEO friendly path instead of the default path, without hardcoding the path in the links in the view (adopt from currently URL, more or less). For example:
#Html.ActionLink("Edit", "Edit", New With {.id = currentItem.ID})
That creates an Edit link like : /MVC/Edit/120
But, what I would like is: /article/edit/120.
Any help appreciated.
Use
#Html.RouteLink("Edit", "myRouteName", New With { ... route params ... } );
In ASP.NET MVC, for linking standard pages (the ones that does not need special parameters or query string), is there any situation where I should prefer Html.ActionLink() to a standard Link tag?
Thanks.
#Html.ActionLink is tied into the MVC routing definitions. It isn't just a helper for writing an anchor tag, it uses routing to determine what the href looks like and how it's structured.
By using ActionLink you insure all your links are rendered based on how your MVC routes are configured.
Routing is powerful and can prevent the need of having to have lots of query string variables or hidden fields to pass around data.
Use #Html.ActionLink, in the end the extra effort is negligible.
Its a matter of preference.
Html.ActionLink() is just a HTML Helper which will ultimately render a <a> tag.
I prefer using <a> tag.
I need to implement an application with multi-language support using an AngularJS front-end and a Ruby on Rails server.
I am looking for a reasonable approach to render translated templates in multiple languages. I have come up with an approach I would like feedback on.
In the Angular routes definitions, set the template property to an html partial that just has an ng-include with the src attribute value set by the controller. This approach is needed to dynamically modify the path to the template to be fetched from the server; it is described here:
AngularJS - How to use $routeParams in generating the templateUrl?
So the Angular route config would look like:
angular.module('myApp', []).
config(function ($routeProvider) {
$routeProvider.when('/sites/new', {
template: '<div ng-include src="templateUrl"></div>',
controller: 'RouteController'
});
});
And the controller would look like:
// Set a prefix on the URL path, something like “es”
function RouteController($scope, $routeParams) {
$scope.templateUrl = $routeParams.locale + "/template/sites/site";
}
Here $routeParams.locale is used to set the locale, but could be a variable set by user action. The approach for dynamically modifying the template URL path to add a locale prefix seems a bit convoluted, but I know of no other way.
On the Rails side, in routes.rb, add a route:
match '/:locale/template/*template' => 'template#get'
The route uses route globbing so the params[:template] value can be a multi-level path.
The TemplateController#get action just renders the partial determined by params[:template]
The Template controller code is something like:
class TemplateController < ApplicationController
layout false
caches_page :get
def get
render(template: "template/#{params[:template]}")
end
end
The Rails I18n support for translations is used in the erb templates, translating according to the locale parameter.
In production, caching would be turned on. This would avoid incurring translation overhead. The locale prefix of the URL path would result in a per language set of translated templates to be cached.
This approach pushes translations processing to the server side as much as possible.
Is there any fundamental problems with this approach?
Could it be done better?
You may be interested in the angular-translate module.
We achieved client side internationalization in AngularJS with the i18next JQuery plugin http://i18next.com/ and created a filter called i18n.
When you initialize the Angular Application, you initialize the i18n plugin where you can provide a pattern to locate the file containing labels, and in the template use this as an example for binding labels and values.
{{'mynamespace:labels.firstname' | i18n}}
where "mynamespace" is used to separate your labels logically and used to locate JSON files with the labels. Within json files you can have one or more JSON objects with labels as properties. In the above example, the file called mynamespace-i18n-en-US.js if you provided a pattern __ns-i18n-__lng__.js
Is there a reason why the translation has to happen on the server?
Do you really need to translate the entire template?
What is the benefit to using Html.BeginForm?
We're having some issues related to html helpers and razor generator view testing and I'm struggling to see the benefit which would stop us going back to old skool form tags.
Has anyone got an argument for or against either?
by old skool i mean:
<form action="#Url.Action('Blah')">
The Html.BeginForm is useful because it generates the url using the routes defined in the Global.asax. (or you can extend it with your own code)
Using the old tag is neither worst or best in my opinion. You simply have to generate your url manually or using the Url helper. In the end the html in the page will be the same
<form ....>
html
</form>
Html.BeginForm also implements IDisposable, meaning the form must be closed properly. It's a minor thing, perhaps, but not closing Html.BeginForm produces a run-time error, where an unclosed <form> tag does not.
no there is no difference , the form tag just use the routing to generate the url , so if you use #Url.Action you are good to go
there is even books use that way a plain old tag and a url helper to generate the route
ASP.NET MVC Website Programming is an example
Edit
**
starting from Mvc 4 there is no difference , prior to Mvc 4 , Mvc 3 for example require the Html.BeginForm to make the javascript unobtrusive validation to work
We have been trying to implement shortcodes on an ASP.NET MVC web app that allow users to uniquely invoke a given article/page using an assigned short code.
For e.g.: www.mysite.com/power would map to an actual URL: www.mysite.com/Power/Home/.
I have created various routes throughout the site that map these shortcodes to various actions and controllers within the application. From a shortcode/route point of view, everything is working great.
I, however, noticed a couple of interesting things. I have hyperlinks that I use Url.Action method to generate the URL pointing pages. Many of these pages also have short codes associated with them. For e.g.: I have a link that says:
Go to Power page
This is a page that also has the previously mentioned short-code assigned to it. When I use Url.Action, I ideally expect it to create a link as /Power/Home/Index or /Power/Home, but since I also have a route constraint mapped to it, it now generates the link as /power.
Is there a way I can just use the actual link URL when generating links? I only want short-codes when I am sending out emails etc. I want the site to generate actual URLs.
This may or may not be possible, but I wanted to see if there were any ideas out there that I could use.
Anup
Index and Home are likely defined in your route table as defaults for the Action and Controller element. When you generate the Url it wont include the defaults if they aren't needed.
You could write your own Action overload or helper, which would allow you to take more direct control of the generated URL or action link. You could approach it from two different ways: 1) a helper to generate short-code specific urls and links, and/or 2) a helper to generate the full url and/or link. If Url.Action is returning the short-code version due to your routing configuration, I'd think a good place to start would be the second option, creating a helper/extension method that will generate the full url for you.
Here's how I solved this:
Instead of naming a route with short code to point to the action url, I made the route point to a different Controller action which would then redirect to the actual route that I want it to.
For e.g.: Originally I had the code "power" defined in the route table such that it would point to www.mysite.com/Power/Home.
Now instead of pointing it to that action - Index, controller - Home, area - Power, I make it resolve to: action - Power, Controller - Home, Area - ShortCode.
In the controller now, I simply do a RedirectToAction("Index", "Home", new { Area = "Power" });
This ensures that the actual links to /Power/Home do not resolve to the shortcode "power".
This is a simple fix increased the work by a little bit, but works like a charm.