c# - How to handle 'View not found' InvalidOperationException in asp.net mvc? -


I have an asp.net mvc 1.0 site that serves some content from 2 level hierarchy / category / article < / P>

When things work right for a view of the article and the scene is provided. However, when the URL meets the routing status, but the view is not present, the exception is raised so that I can not go into the operation of the controller.

Routing:

  Routes.MapRoute ("paragraph", "{class} / {article}. Espx", new {controller = "microcontroller", action = " Paragraph "});  

MyController verb:

  public action paragraph (string category, string text) {string path = string.format ("~ / scene / {0} / {1} .aspx ", category, article); ViewResult vr = View (Path); Return VR; }  

However, when the view is not found, the system. Resident Operation Exposure is generated that I can not hold in the controller action.

Exception Description: System.InvalidOperationException: The view '/ / views / my-category / my-article-with-long-name.aspx' or its master was not found. Searched at the following places: ~ / views / my-category / my-article-with-long-name.aspx

I get the error to Application_Error () Method global.asax.cs :

  1. Do not know how to redirect error view from there
  2. Wonder if there is a better The place where the exception has been raised.
  3. XD, Greg, I appreciate your answer. It helps to take advantage of a well-researched solution that everything I have to do is an override controller. Since I only have one controller, where I need to behave, I need to override only the uppercase in that controller.

    It is being said, my solution does not consider symptoms as a disease, and as you both suggest, it will be better to check the existence of the file before implementing it. See on the path.

    Here I have used the code to treat the symptoms :)

      Protected Override On Option (Exception Consultant Filter Contains) {// Invalid Operation Exception is thrown If the path of the view can not be resolved by // calculation, if (filterContext.Exception invalid operation exception) {filterContext.ExceptionHandled = true; FilterContext.Result = View ("~ / visible / error / NotFound.aspx"); FilterContext.HttpContext.Response.StatusCode = 404; } Base.OnException (filterContext); }  

    A problem that I could not resolve, is a way to display NotFound View in a clean way. This is usually used through the error controller NotFound Action. I had to hardcode the path for this. I can live with it, but I would like to know whether it is possible that HC Path.


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -