0

I have the code below, which works fine. Suppose I now want to change something in the ActionLinkButton method, like add another parameter to its signature. I would then have to change the call in .cshtml as well, of course. However, there is no compile error if I don't change the .cshtml, the application just crashes when attempting to display the View, getting directed to the default error html page.There is no detail of the error in the logs, in firebug, anywhere. Obviously, I should try to remember to check all calls when I make such a change, but I forget.
So, how can I prevent this error or make it easier to trace?
1. Some sort of Razor syntax checker?
2. Some way of directing to an error page with some meaningful message?

.cs

public static MvcHtmlString ActionLinkButton(
         this HtmlHelper htmlHelper,
         string buttonText,
         string actionName,
         string controllerName,
         RouteValueDictionary routeValues,
         Boolean enable) 
     {
         string href = UrlHelper.GenerateUrl("default", actionName, controllerName, routeValues, RouteTable.Routes, htmlHelper.ViewContext.RequestContext, false);
         // title is tooltip
         string buttonHtml;
         if (enable)
         {
             buttonHtml = string.Format("<input type=\"button\"  title=\"Export\" value=\"{0}\" onclick=\"location.href='{1}'\" class=\"button\" />", buttonText, href);
         }
         else // disable button
         {
             buttonHtml = string.Format("<input type=\"button\"  disabled=\"disabled\" title=\"Export\" value=\"{0}\" onclick=\"location.href='{1}'\" class=\"button\" />", buttonText, href);
         }
             return new MvcHtmlString(buttonHtml);
}

.cshtml

@Html.ActionLinkButton(
             buttonText: "Export",
             actionName: "Export",
             controllerName: "Detail",
             routeValues: new RouteValueDictionary(new {PermitId = @Model.PermitId }),
             enable : false)
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
  • have you debug your code, verify the parameters of method its maybe here the error. – Enrique YC Jan 02 '15 at 20:20
  • 1
    Edit the Web.config file to turn off customErrors mode. – Casey Jan 02 '15 at 20:47
  • Yes, this does give accurate information. Thanks. I had had the Web.Debug.config file set this to off, but this was insufficient. I had to do it in Web.config. – Al Lelopath Jan 02 '15 at 20:54
  • 2
    Some third party tools (`Resharper` and `Just Code` from Telerik) validate cshtml files tool. There is also an option in Visual Studio to turn on pre-compiling of views which will probably help you more. – iCollect.it Ltd Jan 04 '15 at 22:53

1 Answers1

1

You can compile your Razor views at build time so you will get razor compile error before run.

Set <MvcBuildViews>true</MvcBuildViews> in the <PropertyGroup> element of your .csproj file.

How to compile cshtml before runtime

This can be slow, I enable it only in release.

Community
  • 1
  • 1
Guillaume
  • 12,824
  • 3
  • 40
  • 48
  • This causes build error: Error 3 It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory not being configured as an application in IIS. – Al Lelopath Jan 09 '15 at 18:39
  • Fix is to do as Chris Hynes suggests here: http://stackoverflow.com/questions/4725387/mvcbuildviews-not-working-correctly – Al Lelopath Jan 09 '15 at 20:31