RATCHET

RATCHET Labs

02/15/2011

view or no view…that is the question.

I recently ran in to a problem opportunity where some of my pages had a their own template and all generic content pages used their own template.  Because they were all routing through the same controller and action I wanted a solution where if a view did not exist for the page in question it would default to the generic view.  Here is how i accomplished this.

 public ActionResult Page(string viewName) {
            
                ViewEngineResult viewResult = ViewEngines.Engines.FindView(ControllerContext, viewName, null);
                if (viewResult.View != null) {
                    return View(viewName, new PageModel(viewName));
                }
                else {
                        // Here you would want to make sure the page exists in your CMS or where ever
                        return View("Index",  new PageModel(viewName));
                       // If page does not exist you can
                       // throw new HttpException(404, "404 - View Not Found");
                       // or 
                      // return View(viewName);  
                      // and add the exception handler below


                }
            
            
        }

Once you check to see if the page exists in your cms or whatever. You can then just return the view that doesn’t exist. But first you must override the controllers exception handler.

        protected override void OnException(ExceptionContext filterContext) {
            //InvalidOperationException is thrown if the path to the view
            // cannot be resolved by the viewengine
            if (filterContext.Exception is InvalidOperationException) {
                filterContext.ExceptionHandled = true;
                filterContext.Result = View("~/Views/Page/NotFound.cshtml");
                filterContext.HttpContext.Response.StatusCode = 404;
            }

            base.OnException(filterContext);
        }

There might be cleaner ways and more elegant way to do this so let me know. For now this works for me.

Cheers

  • Facebook
  • Twitter
  • Digg
  • Print
  • email

01/20/2011

MVC 3

Starting to write some .NET application using MVC 3 and the Razor engine. It has been a while since I used MVC pattern on web application. I think the last attempt at it was with ColdFusion and FuseBox. There is a learning curve and a different thought process behind it but its not bad.

Besides the unit testing inherently built in to the framework I see some other nice advantages:

- Front end developers will be able to start integrating with data sooner. Once the Model has been mocked up/defined, front end developers will be able to integrate and write more of the data integration code. Saving middle tier back end developers to work on building kick ass data objects using frameworks of there choice.

- Ditching the view state is plus for the most part. Not having to set the “enableviewstate” property and controlling the some of the state yourself gives you a lot more control.

- Developers coming from other frameworks or languages like ruby, Php(Zend), or Groovy should find this familiar but also get to see how nice the .NET framework is and how Visual Studio can be your best friend.

Check it out download a few test apps.

http://www.asp.net/mvc/mvc3

http://haacked.com/archive/2011/01/13/aspnetmvc3-released.aspx

  • Facebook
  • Twitter
  • Digg
  • Print
  • email