3

My question is regarding handling errors like "missing an assembly reference?" in views specifically layout.cshtml

e.g. if I have some code in Head tag in layout.cshtml

<head>

@{
  @{

        try
        {
            var EnableSomething = BusinessLogic.MyBLL.GetSetting("SettingName");
        }
        catch { }
    }
}

</head>

This will work properly, but what if someone came and change "MyBLL" to "MyBLL1", in Layout.cshtml or remove the BusinessLogic DLL or change the DLL in a way that "MyBLL" does not exist in that DLL.

If there a way to handle this kind of situation in layout.cshtml itself ? I know this is a strange situation but just wanted to know if there is any way to handle this kind of situation.

Thank you all.

Deepak
  • 428
  • 5
  • 12
  • 1
    That code belongs in your controller, not a view. –  Jan 26 '18 at 06:47
  • MyBLL is class? BusinessLogic is namespace? – D-Shih Jan 26 '18 at 06:54
  • @StephenMuecke you are right, this code should be in controller but in this case it is in view ( layout.cshtml ). and it gives the compilation error when anyone request the page in above situation. – Deepak Jan 26 '18 at 06:54
  • @daniel.shih yes, that is correct. – Deepak Jan 26 '18 at 07:05
  • 1
    @Deepak you already know what your problem is. The code is, where it should not be. Move it and get compile time errors. Pass the information into the view using a viewmodel or ViewData – Marco Jan 26 '18 at 07:17
  • @Marco you are right, and i am working on moving the code from view, but in the mean time i wanted to know if there is any way to handle this kind of error in view itself. – Deepak Jan 26 '18 at 07:22
  • I know it's late but for anyone who is wondering how I solved this is, to move the code in controller and get a viewdata/viewbag which will have either setting value or null, so there won't be any exception which i cannot handle at view level. – Deepak Sep 05 '18 at 19:27

1 Answers1

1

You could try and install the Nuget of RazorGenerator

It will cause the build to break if building a view fails , So in the case of "MyBLL" to "MyBLL1"

Lord Darth Vader
  • 1,895
  • 1
  • 17
  • 26