Is there a general ASP.NET MVC helper for generating links? - asp.net-mvc

I know of the ASP.NET MVC ActionLink helper for generating links to the application's actions, and find it very useful. However, I haven't been able to find a corresponding helper for generating links based on absolute URLS (i.e., to external resources). Is there a helper for this purpose?
Edit:
To make my question absolutely clear, I'm (obviously) not looking for a way to generate the URL part, in the way that ActionLink generates action URLs. I'm just interested in safe and easy hyperlink generation.

I'm afraid there isn't. But you can write your own helper for that if you want the view to look nice with the consistent Helper formatting.
http://www.asp.net/mvc/tutorials/creating-custom-html-helpers-cs

You don't need a helper for this, the helper for your action links is so that your routing works and you get nice urls. You need to know the external urls so there is no real shortcut other than putting them in config.

Why not use an <a> tag when you have absolute url?

No, Url.Action for example is the one used to generate Urls from routes which are registered in Global.asax.cs but since you said external and absolute how come will there be a Generator for them?

There isn't, since there is no special routing information in an absolute URL that is beneficial to use a helper to generate. You can simply use standard HTML markup - which, frankly, is far clearer to read and more customisable than helper markup. However, if you really would prefer to use a helper then you can always make your own.

Related

URL rewriting in asp.net MVC 4

I would like to make my app url like http://domain.com/product/product-name....
Is it possible that is if I am passing productid and can get productname in my url?
URL Rewriting is the practice of "faking" a URL by rewriting it to look like another URL. It does so in a single direction. This means it is has no built-in way to generate a URL for use on the UI.
.NET Routing is what MVC applications typically use. It is a real, not faked 2-way mapping from URL to a set of route values and/or a set of route values to a URL. This means that you can easily generate URLs for use within the application using HTML helpers such as ActionLink, and as such is the recommended and preferred approach for building custom URLs from within the application.
Sometimes both of these techniques are used, but URL rewriting is generally only put in place to do things that make sense to change from outside of the compiled application, such as when nesting the application within another application or putting in one-way 301 redirects for legacy URLs.
To answer your question, yes it can be done, but should be done with .NET routing, not URL rewriting. To use names like this, you need a mapping between name and ID. A couple of ways this could be done:
Make a Name-ID mapping right in your route configuration by using static route segments with the name and route values that contain the related ID. This approach will work only if your URLs don't need to be changed dynamically within the application.
Use a Name-ID map within a cached dictionary object. Use a custom RouteBase implementation to do mapping in each direction by overriding the GetRouteData and GetVirtualPath methods.

Does T4MVC support WebApi?

I'm trying to use T4MVC to create strongly typed links to WebApi endpoints, and while it does generate helpers for my ApiControllers, there's no method for creating links. The generated controllers have ViewNames, but no action methods. I guess this is because ApiController's don't return ActionResult's, but what do I need to be doing instead to create these links?
It is not supported today, but please check out this thread which has some good amount of discussion about it. Feel free to reply over there to add to it.
Still doesn't seem to be supported by the end of 2015.
If it's URL generation that you're concerned with, then take a look at Drum if you're using attribute routing in Web.API 2+. And if you've got routes set up in your application startup (Web.API 1) then it appears Hyperlinkr will be helpful.
If you need other things (like a hard-coded parameter list) then you're still out of luck.
Disclaimer: I've not used either

ASP.NET MVC - What is the benefit of using Url.RouteUrl or Url.Action versus just using the URL directly?

What is the benefit of using Url.RouteUrl or Url.Action versus just using the URL directly?
If you change your routing configuration, by using Url.RouteUrl or Url.Action your generated Url's will update along with your routing configuration.
I find the real benefit comes when tied together with T4MVC. Then I have strongly typed access to my Action routes, so if my controller's change the compiler alerts me if any of my Url's need to change as well.
It's very simple to set up custom routing rules to create so-called "pretty" (or SEO-friendly) URLs. If you ever change one of those URLs, you don't want to have to go through your entire application and change it every time you link to that action. On the other hand, if you use Url.Action, it will change automatically.

Design Practices - Several Pages with ASP.NET MVC

Is there a specific design practice for MVC type sites that need to have a lot of non-model related pages? I mean, it seems very silly to make a controller action for every single page; Yet at the same time, that seems to be the only way to realistically do it and adhere to standards. Is there any documentation or examples available for things like this?
When I speak of non-model pages, I mean things that are just display; Static information that you might use a standard HTML website layout for. But it has to be intermingled with other parts of the site that do require models and validation/etc.
Create a folder for your static content, and put in an ignore route for those pages. This causes those pages to be passed through directly to IIS for immediate display.
routes.IgnoreRoute("StaticPages/{*path}");
You can also load static HTML content into an existing View. This preserves your ability to work with dynamic content in the same page.
I don't think it sounds silly at all to make an action for every page. That's just how MVC works.
You can ignore some routes, as Robert Harvey suggests, but then you'll have the *.html extension on your static pages but not your internal ones, and you'll be unable to use the Url. and Html. helper methods for linking to MVC actions.
I think you should just go with the flow.
Another alternative was suggested for common static files such as help pages which is based more on a naming convention, but would allow for some flexibility in layout control in the view:
ASP.Net MVC Routing Strategy for Static Content

ASP.NET MVC URL generation extensibility points

What are extensibility points of URL generation in ASP.NET MVC?
Routes - virtual path depends on it
???
Addendum 1
Particularily, I need to control not only path part of URL but the host also. And I'd like to embed my generation logics into MVC Framework so that any call to standard Html.ActionLink method would involve my logics. It would be perfect. However, investigating MVC sources I'm desperate to achieve my goal with an easy way.
Routes is pretty much where it's at. Remember, though, that you can subclass Route and provide your own implementation that does not use key/value URI templates.
There's nothing stopping you from writing your own solution from the ground up, but there's not much point since there's already an extensible foundation for you to work with.
Routing extensibility points
Routes
Route constraints
Route handlers
In your particular case, you will have to write your own route that will populate additional RouteData items related to host. Parsing your URL will be totally on you.
"{host}/{controller}/{action}"
Maybe also create your custom route handler that will make host parameter mandatory. But that already depends on your particular needs.
Edit
I guess that this article about domain routing may be of some help to you. It looks preety straight forward and uncompicated.
Indeed, it's impossible to reach desired flexibility since Route gives just VirtualPath portion during URL generation.

Resources