Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Matt Barlow | jacker.io 164 posts 727 karma points c-trib
    Jul 02, 2013 @ 17:07
    Matt Barlow | jacker.io
    0

    Checking if view exists MVC?

    How would I check if a view exists?

    I have a helper library with static methods, so far I have below, how do I pass the ControllerContext into this?

     private bool ViewExists(string name)
     {
         ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
         return (result.View != null);
     }
  • Andy Butland 373 posts 2057 karma points MVP 4x hq c-trib
    Jul 03, 2013 @ 16:42
    Andy Butland
    0

    Assuming you are calling this from a controller, I guess you'd have to pass it in as a parameter to the ViewExists method.  Or alternatively move the code to a base controller that all your controllers inherit from?  

    If you are calling it from outside of the context of a controller though... not sure - I guess you'd need to be using FileExists and check for the file name on disk?

    Does that help?

    Andy

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies