Does T4MVC support WebApi? - asp.net-mvc

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

Related

Is there a general ASP.NET MVC helper for generating links?

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.

Test Views in ASP.NET MVC2 (ala RSpec)

I am really missing heavily the ability to test Views independently of controllers. The way RSpec does it.
What I want to do is to perform assertions on the rendered view (where no controller is involved!). In order to do so I should provide required Model, ViewData and maybe some details from HttpContextBase (when will we get rid of HttpContext!).
So far I have not found anything that allows doing it. Also it might heavily depend on the ViewEngine being used.
List of things that views might contain are:
Partial views (may be nested deeply).
Master pages (or similar in other view engines).
Html helpers generating links and other elements.
Generally almost anything in a range of common sense :) .
Also please note that I am not talking about client-side testing and thus Selenium is just not related to it at all. It is just plain .NET testing.
So are there any options to actually do the testing of views?
Thanks,
Dmitriy.
The main issue with testing full views is that the asp.net view engine calls Response.Write in the supplied context / and not on the supplied writer.
The above is not the case for testing partial views, so for those you can use this solution:
http://www.brightmix.com/blog/renderpartial-to-string-in-asp-net-mvc/
There are other view engines that do allow you to test the view i.e. Spark.
ps. the concept in asp.net mvc is that you should be able to test the view by using the ViewEngine, but as I understand the asp.net mvc team didn't work around the existing asp.net engine restrictions to be able to do so for their view engine.
You may want to check out the UI Test Helpers that Eric Hexter and the guys with MVCContrib are working on. I haven't had a chance to look at it in depth, but it may help you. I found this link that shows some of the syntax: http://codepaste.net/cw8ie4
I would be interested to know what you find out as I will be doing this pretty soon also.
Interested to know if you find anything for .Net that does this. Our current app is WPF, but we are stuck with trusting Cucumber to touch our Views in all our Features... so yeah, that sucks. Hope you find something and update us.

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.

ASP.NET MVC Conventions

ASP.NET MVC is heavily convention-based, "convention over configuration" as they say. So, this means there's a lot of significance to what name things are given and where in the project structure they are created.
As a newcomer to ASP.NET MVC, I appreciate the power and simplicity of this approach, but I do find it a bit confusing to keep track of what conventions are in play. For example, when using UpdateModel controller method, that relies on HTML form fields having the same names as the properties of the model class. That's the obvious thing to do and most of the time it's probably what most people would do instinctively, but I can see that it would get really confusing if one were to rename something in one place and forget to rename it in the other. The linkage is somewhat 'brittle'.
So, I thought it would be useful to have a list of all the ASP.NET MVC conventions here in one place, as short statements of best practice. things like:
"HTML Form fields should have the same name as Model Properties".
Anyone have anything like that? Will you help me create a list here?
Use the virtual path from your deployment server and enter it in the project configuration in Visual Studio. This way the visual studio development server will use the same path structure as the deployment server. This will save you countless hours of work when deploying.
Controllers should always end with the Controller sufix.
Note that convention over configuration is not required, but is how things work out-of-the-box. If you find a convention confusing or not helpful you can change it (eg. how controllers are picked, how views are located, etc.), or do configuration.
Controller and action names specified in routes should have corresponding classes and action methods named exactly this way (plus "-Controller" suffix for controllers). You can override this behavior using [ControllerName] and [ActionName] attributes.

Asp.Net MVC Identify Site via Url

I have an application that will support multiple sites. The site will be determined based on the url.
For example
http://myapp/site/abc123/...
and
http://myapp/site/xyz123/...
The site code will drive a lot of the functionality for example themes, available modules, etc...
Questions:
1-)I need to validate the site code is valid and if it isn't, it should direct the user to an info page. I was looking at using IRouteConstraint, is this appropriate? Are there other/better options?
2-)Any gotchas with this approach (using url to identify site)? Is there are better approach?
Solution
I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.
The system I have implemented uses Urls to identify unique page content within a single site and the routing process is pretty straightforward. That being said, you may want to consider making use of Areas in your MVC application. With Areas you can have multiple sections to your website that all have their own MVC structure which can run semi-independently.
Essentially, you will have one base routing definition that lays out some defaults and then the rest of the "sites" will define their own routes pointing to controllers and views in a separate location. It's pretty easy to set up, you'll just need to make sure you're using version 2.0 of ASP.NET MVC. Here's a decent looking tutorial on ASP.NET MVC Areas and Routes. In the current model which MVC 2.0 supports you'll have a single Web project for each area, but that is not necessarily a requirement. Phil Haacked has some code for ASP.NET MVC Single Project Areas if you're looking for another example of the technique, although you, personally, will probably benefit more from the first article.
So long as you define good routes that have clear and measurable constraints, you shouldn't have too much trouble laying out the website you've described.
I ended up creating a Custom ActionFilter and check the sitecode in the OnActionExecuting event. That seems to work well and fit better than the IRouteConstraint.

Resources