Find broken controller actions - asp.net-mvc

How do I find broken Html.ActionLinks?
e.g. Find Html.ActionLink("View", "ViewCustomer", "Customer") where CustomerController.ViewCustomer() no longer exists.

You can use strongly-typed action links from MVCContrib.
<%: Html.ActionLink<Home>(c => c.Index()) %>
These will break at compile-time when you remove a referenced controller method, if you are using compiled views.

If you looking for systematic approach install (can use NuGet) and apply T4MVC.
I did it to my own project - all your magic strings (not only action links, but everything that require strings) disappear across your application. You end up using only strongly typed helpers to eliminate the use of literal strings.
In your case in particular
#Html.ActionLink("View", "ViewCustomer", "Customer")
will become
#Html.ActionLink("Externalize me as well!", MVC.Customer.ViewCustomer())
and if you externalize the one suggested, it will become what you looking for:
#Html.ActionLink(Config.ViewLabel, MVC.Customer.ViewCustomer())
Isn't it a beauty?
I think this supposed to be de facto standard rather than "stringified" approach.
Look to what it does to your project:
In Views:
#Html.RenderPartial(MVC.Customer.Views.YourView)
In controllers:
return View(Views.YourView);
or
RedirectToAction(MVC.Home.MyAction().AddRouteValues(Request.QueryString));
Hope this helps.

The accepted solution does not answer the question. Here's my solution (not sure if it requires Resharper or not).
Click on any broken action (create one if you must)
Hover the red bulb
Hover Inspection MVC
Hover Find similar issues in >
Click custom scope
Restrict your scope (it might tell you 10000's of quality issues)
Search "resolve action" in the results

Related

Ember: How to link to sections and actions (without nesting routes)

Let's say that I have a blog where each post can have several sections and comments and I'd like to use a hard-links to navigate and operate on this. There are several samples using some pseudo-code, of course they doesn't work, just demonstring my intends :)
Of course /blog.html#/posts/1 uses PostRoute, PostController etc and uses :post_id for finding object - that's obvoius.
How can I pass (and then access) additional params which doesn't change the controller but I can use them to navigation. ie /blog.html#/posts/1?section=123 should use the same route, controller and view as it was just Post, but I'd like to read the section and just navigate to section with #123
/blog.html#/posts/1/?comments=456 - actually should behave like section from point 1, but navigates to comment and optionally add some class to the container.
Other case: I'd like to go to section 123 AND additionally edit it with link like: /blog.html#/posts/1?section=123&action=edit. Now I'm using a button with an action like {{action editSection section}} and {{#if isEdit}} but I'd like to be able to reflect this in URL and also go to this state from URL (de facto my post can have several different modes not only preview/edit, therefore it should be accesible by the link).
I hope that cases makes sense, TBH I have no idea in which direction should I go. Tried with nested routes, but I'd like to avoid changing the controller. Also have no concept how to reflect the action in the URL...
I'm using Ember 1.1.2
You can use the model method of the route to handle such parameters, separate them from the model parameter and set the appropriate controller state.
Another approach would be to use nested routes that will render un-nested views(and controllers) - as explained towards the bottom here.

Where to put reusable HTML code in rails?

I'm writing a rails app that allows users to delete records of various sorts. After pressing a delete button, I'd like to show a confirm dialog using bootstrap. I'd like to use this same dialog in several of my views, so I'll need to include the same HTML snippet in most of my pages.
I'm new to rails and I'm still learning the conventions. Can anyone suggest the best (or standard) place to put the dialog code? Should it be a partial in views/layouts/_confirm_delete_dialog.html.erb, should it go inside application.html.erb, or should I put it somewhere else?
Thanks in advance for your advice,
D.
Within your Views folder, you can create a general folder (called whatever you want). If you have a variable that you need to pass through to the general layout, you can definitely do so, but you will want to make sure that the information that you're passing through doesn't cause conflicts with the fields pulled from the model. For example, if you have two models and one has a public field but the other doesn't then you will not want to have a generic message that is using the public field. However, something like the created_at or updated_at would be okay.
You would use a code similar to,
<%= render 'general/simple_message', :f => f %>
In your views folder, you would have a directory called general and a file called _simple_message.html.erb.

Collapse C# block within cshtml and save it

We have a bit big blocks of C# code within our cshtml files which must be presented in cshtml and nowhere else (obviously it's not a brilliant case but it's another question).
How we can collapse or hide these blocks of code in order to let our designers work more smoothly? We also want to hide these blocks of code during the demos of the progress with markup.
The real issue is that we also must save the visual representation into SVN.
Is there any native VS 2010 functionality for this or plugin? Maybe there is an opportunity to use "partial" cshtml pages where all the markup will be in one file and all C# code will be in another?
Unfortunately VS isn't going to collapse C# blocks of code within #region directive in such files.
Ultimately there is a similar question Regions In ASP.NET Views? but it gives no answer on how to save the collapsed representation when "Collapse Tag" context menu action item was used.
Try using Visual Studio's collapse functionality. By default I believe the keys are:
[Ctrl+M,Ctrl+H] to hide an arbitrary selection, and
[Ctrl+M,Ctrl+U] to unhide the same ( while collapsed ).
This should allow you to temporarily hide any code. More details available on MSDN
Is this what you were looking for?
Having read a little further you wish to save them collapsed, and apparently .cshtml doesn't support #regions. I guess a hacky solution might be the old:
#if(false){
<div>
<!--/*{your long code}*/-->
</div>
}
Or something to that effect, but you get the idea :)
Just select your code, right click and select Collapse Tag
The way that I see it is that cshtml files are meant for a "user control" side of a presentation layer. If you have too much code in your view files, then I would refactor the code and move re-usable components into partial views. I would then include these partial views through
#Html.RenderPartial("PartialViewName", Model.propertyToRender), or I would use
#{ Html.RenderAction("ActionName", "ControllerName") ;}

Should I make a ImageHelper in this situation?

Hi I'm working with a project (asp.net mvc) where I need to show pictures on one site. They gone have jquery and be surrounded by a div like <div><img/></div>
I'm relatively new on MVC so I'm not sure what ways are the best to work in it yet. Should I do a ImageHelper so i can access it like <% Html.ImageJquery() %> or should i just do it plain in the view
what are your thoughts on this?
I am not sure if there is any single best practice for this case. As you already said you could create an extension method on a HtmlHelper or just put the code in the view.
Because the "added" code is a very simple (just two divs) I would skip extension method and just add the divs in the view. But if the code is actually more complex I would create a helper.
Cheers!
It depends how often you would use the helper. If it is used in a couple of places it would make sense, because it helps you to reduce redundant code.
The other option you have are partials.
I would go with an Extension method on the HTMLHelper , so that it accepts the 'src' value as a parameter and constructs the image tag accordingly.
Regarding Rendering of UI elements , i did read a blog post on 'Conditional Rendering' which i thought you would find it interesting .
please check the link below .
Conditional Rendering
Thanks ,
Vijay.

Using T4MVC in real project

T4MVC is cool, but I have a couple of issues integrating it in my project, any help is really appriciated:
I've got such warnings for all my actions (I use SnippetsBaseController as base class for all my controller classes:
Warning 26 'Snippets.Controllers.ErrorController.Actions' hides inherited member 'Snippets.Controllers.Base.SnippetsBaseController.Actions'. Use the new keyword if hiding was intended. C:\projects_crisp-source_crisp\crisp-snippets\Snippets\T4MVC.cs 481 32 Snippets
Is it possible to have strongly typed names of custom Routes, for example, I have route defined like this:
routes.MapRoute(
"Feed",
"feed/",
MVC.Snippets.Rss()
);
Is it possible to replace:
<%= Url.RouteUrl("Feed") %>
with something like:
<%= Url.RouteUrl(MVC.Routes.Feed) %>
Having strongly typed links to static files is really cool, but I use <base /> in my pages, so I don't need any URL processing, can I redefine
T4MVCHelpers.ProcessVirtualPath without tweaking the T4MVC.tt itself?
T4MVC always generate links with uppercased controller and action names, for example:
/Snippets/Add instead of /snippets/add. Is it possible to generate them lowercase?
Can you change your base controller class to be abstract? I think that'll fix it.
See this post which discusses this: http://forums.asp.net/t/1535567.aspx
If you look in t4mvc.settings.t4, you'll see a ProcessVirtualPathDefault method that you can change without touching the main .tt file.
See http://forums.asp.net/t/1532057.aspx. There is suggested fix in there, though it has not yet been submitted (but you can try it).
David

Resources