Brendan Enrick

Daily Software Development

Simple CMS Released

    After much anticipation, I am sure, from my zealous readers, I offer to you Simple CMS. This is a simple content management system, hence the name, which plugs into existing applications. This ASP.NET 2.0 plug-in is not meant to be a full blown application. This is for someone who has a site, and wants to easily add content to the site. With Simple CMS you can through a web interface create content for you pages, and control the addresses of these pages within your site.

    Simple CMS is currently in Beta, but support will be offered most prominently through the Simple CMS Forum, which is also where I would like people to let me know of any features they would like to see in later versions. I already know of some large changes I plan to make for future versions of Simple CMS.

    I hope you enjoy Simple CMS, and you can expect some articles to be written and posted on ASPAlliance.com about how I made Simple CMS. I will probably write these as separate articles about interesting and useful topics I employed in the creation of my content management system. If you think Simple CMS is cool, you should also check out this Cache Manager on which Simple CMS is loosely based. It is a very useful little tool.

    Steve Smith did a nice release post for Simple CMS yesterday, so rather than duplicate the content I will just link to his post.

    Thanks for reading my Blog. Keep reading it and maybe you will get a free cookie, but you probably won’t.

My Review of one of AppDev's training courses

    Don’t worry. I am still around, and I have just written my review of AppDev’s Professional Training via DVD-ROM course called Microsoft® Visual C#® 2005: Developing Applications. It was a very good course, and I would recommend it to others. My review is called Review: AppDev Visual C# 2005: Developing Applications. To write the article, all I had to do was go to ASP Alliance.com and click on the link entitled Write for Us. From there I was able to submit a request for the article. It was accepted shortly afterwards, and I soon after started writing the article by following their set of article guidelines. For those of you who read my blog, expect to read more of my articles on ASP Alliance.

Embedded Resources not so painful anymore.

    Well after a frustrating time trying to figure out how to make embedded resources work, I finally understand. This makes everything much easier. As some of you may know I have been working on a content management tool. (This is not a full blown content management system) Since it is designed to be portable to existing ASP.NET web sites, it is just a dll file. Even though it uses a couple of images, I don’t want anyone to have to copy image files just to get the app working correctly.

    To accomplish this I decided to use embedded resources, something I had never done before. Starting out I fumbled a lot trying to figure out how to display these, bad idea. Well I started googling for a solution to my problem, and I tried doing what Microsoft said to do. Following what they said to do got me nowhere, and left me a bit more confused. The site makes many assumptions about what I all ready know. Since I did not know these things, I took some guesses. Sadly incorrect ones. I think the authors of those resources need to learn the value of a hyperlink. It would allow them to reference old material so I could learn what they assume I know.

    After reading that I had a better reference to start googling. I started looking for the GetWebResourceUrl method, and this led me to other web sites, which had some better explainations. At first I went to an article on codeproject explaining how to access embedded image resources. This was much better than the previous one I read, but I still could not get this one working right. My example didn’t quite fit with what they did, and they didn’t explain each part of the process.The problem is that even if I had incorrect code in my file, GetWebResourceUrl still returned a url that looked like it was supposed to. Again I needed to find something to get this working, or I would have to go back and start writing a lot of code I did not want to write.

    So I tried another link I found on google, and this article on Embedding Resources in ASP.NET 2.0 Assemblies explained to me very well how to get my embedded resources working. It explicitly told me where each part of code belonged It mentioned some specifics I might need that were not even part of that example. Not only that but it had more than one type of embedded resource that it accessed. If you are ever trying to use embedded resources I recommend it, but I suggest reading this article first. I may have to start reading more articled written by Mark Hines. If you are interested in this topic, I suggest reading this article as well Embedding Resources in ASP.NET 2.0 Assemblies - Part 2.

Caching made easier with a cache manager

   I recently added caching to my Content Management System. Since I am no caching expert I wanted to make sure I had created the functionality that I wanted to make. I wanted to ensure durations were as I expected, and wanted to make sure that items were cached when they should be and removed when they shouldn’t be cached anymore. I could have changed the program to debugging mode and debugged the code. I could also have added code to check and see the values of the cache object at certain times, but I found a much easier solution.

   I found that by adding this Cache Manager plug-in to my site I was able to quickly and easily check at any moment what was in the cache. All it took to add this to my site was copying this file into my bin folder, and copying a line of code into my web.config file. Since I was just using this while debugging the application there was no need to worry about the optional security on the cache manager. When I was done setting up the caching, I simply removed the dll file and the line of code from my web.config file.

   I recommend checking out that cool little plug-in if you are doing any work with caching. I plan to use that tool any time I am working with caching on any ASP.NET web site. I like things that make my job easier.

Simple CMS

    Lately I have been working on a simple content management system. This CMS will not be like a lot of what is currently available. The software applications that currently exist tend to be applications which are the site. Simple CMS will plug into an existing web page. This is beneficial because it will allow people who have one or more web sites to quickly and easily add content to that site.

    So far I have been able to keep the install down to one executed SQL script, a dll file, and some copying and pasting into a web.config. This may be changing a bit as I add more to the functionality of the system.

    I have been working with the httphandlerfactory class. I have my SimpleCms handler factory inherit from that class, and I use to handle the content which the system creates. Creating these pages often gets complicated, because I have to find ways to get the controls to render correctly. Sometimes I cannot use the controls I want to use because I cannot get them to render. I am working still to get them working, but for the time being I want to have a working CMS.

    Inheriting from System.Web.UI.Page, I create my base pages for the system, and from there I have a couple of custom administrative pages built into the system. This is the interface which users connect to to manage the content. Since these pages are classes the user is able to change the location with which to connect to them.

    Soon I will be passing this assembly to Zach Bussinger, who is going to try to implement and use this. I like to think that he is a test subject or guinea pig. If he can't get it working I think I need to go back to the drawing board, but if he can then it should be usable. He will also probably let me know what kinds of problems he finds in the application that I did not notice.

    Once I have a version of the application to release I will make sure to blog more about it. I will probably talk more about how to use it and its features when I am farther along with the project.

Brendan Enrick | Types of ASP.NET MVC 3 Action Results

Brendan Enrick

Daily Software Development

Types of ASP.NET MVC 3 Action Results

This will surprise some of you that know me or the company I work for, but not all of our staff are experts with ASP.NET MVC. In fact, I am hoping that the couple who aren’t will read this post and learn a little bit more about the topic.

Since the actions of controllers in MVC are dealt with constantly, I think it is a good place to start. This post is going to briefly describe the different types of results that are available to you in ASP.NET MVC 3. I will show some of the code that makes them work, which should make all of this seem a lot less complicated.

When creating new controllers in ASP.NET MVC 3, they will come with one or more actions by default. This depends on whether you selected a template which includes extras for you. The Empty controller template comes with an Index action with a return value of type ActionResult.

ActionResult

The action result is a very generic return value for an action. This is because it is the abstract base class for other types of actions. It is actually a very simple class having only one method that needs implementing.

public abstract class ActionResult
{
public abstract void
ExecuteResult(ControllerContext context);
}

Inheriting from the ActionResult are the following classes:

  • ContentResult
  • EmptyResult
  • FileResult
  • HttpStatusCodeResult
  • JavaScriptResult
  • RedirectResult
  • RedirectToRouteResult
  • ViewResultBase

These are the classes that inherit from ActionResult indirectly:

  • FileContentResult
  • FilePathResult
  • FileStreamResult
  • HttpNotFoundResult
  • HttpUnauthorizedResult
  • PartialViewResult
  • ViewResult

ViewResultBase, ViewResult, and PartialViewResult

The ViewResult is the most common concrete type you will be returning as a controller action. It has an abstract base class called ViewResultBase, which it shares with PartialViewResult.

It is in the ViewResultBase abstract base class that we get access to all of our familiar data objects like: TempData, ViewData, and ViewBag.

PartialViews are not common as action results. PartialViews are not the primary thing being displayed to the user, that is the View. The partial view is usually a widget or something else on the page. It’s usually not the primary content the user sees.

This is the common return syntax, and it means that you’re returning a ViewResult.

return View();

That is actually a call to the base Controller.View method, which is just going to call through with some defaults.

protected internal ViewResult View()
{
return View(null, null, null);
}

 

The beauty of ASP.NET MVC is actually in its simplicity though, because all that really did was create our ViewResult for us. If we take a look at the method that is being called you can see that we’re just taking a little shortcut and keeping our action clean of this code we would otherwise repeat every time we wanted a ViewResult.

protected internal virtual ViewResult View(
string viewName, string masterName, object model)
{
if (model != null)
{
ViewData.Model = model;
}

return new ViewResult
{
ViewName = viewName,
MasterName = masterName,
ViewData = ViewData,
TempData = TempData
};
}

Notice how simple that really is. All it did was put the model data in if we specified it, give the ViewResult the Controller properties that we set already, and assign the viewName and masterName.

Keep in mind, that we already saw that the abstract method in the ActionResult was the ExecuteResult method. The last two things to look at with the ViewResultBase are the ExecuteResult method and its abstract method FindView, which is being implemented by ViewResult and PartialViewResult.

public override void ExecuteResult(
ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (String.IsNullOrEmpty(ViewName))
{
ViewName = context.RouteData
.GetRequiredString("action");
}

ViewEngineResult result = null;

if (View == null)
{
result = FindView(context);
View = result.View;
}

TextWriter writer = context.HttpContext.Response.Output;
ViewContext viewContext = new ViewContext(
context, View, ViewData, TempData, writer);
View.Render(viewContext, writer);

if (result != null)
{
result.ViewEngine.ReleaseView(context, View);
}
}

This method is also not very complicated. It checks to make sure we have context, and then if we don’t have the ViewName then we get that information from the RouteData. Remember in MVC that the name of action is included in the RouteData, so we can use that as the default view name. This means that in the Index action, if we just call View(), it will give us a ViewName of “Index”.

We then get the view we’re looking for by calling the FindView abstract method, which means we’re calling through to either ViewResult and PartialViewResult. Those I am not going to get into the guts of, but each one is going to try to find the correct view based on the name using its collection of ViewEngines.

Once we have the view, we are able to tell it to render itself using the context and the TextWriter we give to it.

That’s all there is to a ViewResult.

ContentResult

The content result lets you define whatever content you wish to return. You can specify the content type, the encoding, and the content. This gives you control to have the system give whatever response you want. This is a good result to use when you need a lot of control over what you’re returning and it’s not one of the standards.

Its ExecuteResult override is extremely simple.

public override void ExecuteResult(ControllerContext context) 
{
if (context == null)
{
throw new ArgumentNullException("context");
}

HttpResponseBase response = context.HttpContext.Response;

if (!String.IsNullOrEmpty(ContentType))
{
response.ContentType = ContentType;
}
if (ContentEncoding != null)
{
response.ContentEncoding = ContentEncoding;
}
if (Content != null)
{
response.Write(Content);
}
}

It just puts what you specified directly into the response.

EmptyResult

There is no simpler result than the EmptyResult. All it does is override the ExecuteResult method and since that method is void, the method is empty.

I really don’t think I need that code snippet for this one.

FileResult, FileStreamResult, FilePathResult, and FileContentResult

If you want specific actions to send files as the response, then the FileResult is for you. Sadly, the FileResult is abstract, so you’ll need to use one of the inheriting classes instead. Each of these actually just overrides the WriteFile method for the abstract FileResult class.

If you want to just send the contents of the file back with the array of bytes for the file, then you want the FileContentResult. It uses the response’s OutputStream and writes those bytes directly into the stream sending it down to the user.

If you want to transmit the file using its name, you can use FilePathResult, which will call through a whole bunch of layers finally down to the HttpResponse. Once there it is going to create a new FileStream for your file and write the stream to the response allowing the file to be accessed from your action.

If you’ve already got a stream you can use the FileStreamResult, which will read all of the data from your stream and then write it into the OutputStream to be send back in the response.

These really aren’t all that complicated, but if you want to have control over the file downloads in your application, this is a great way to do it. These give you the power to put any code you want in your action before you give back the FileResult.

HttpStatusCodeResult

The HttpStatusCodeResult is as simple as the ContentResult. In fact, the two are quite similar since they both just directly modify the response object.

This one lets you return any StatusCode you want and you can include a StatusDescription for specifics.

public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

context.HttpContext.Response.StatusCode = StatusCode;
if (StatusDescription != null)
{
context.HttpContext.Response
.StatusDescription = StatusDescription;
}
}

See how simple that is? It’s basically just two lines of code with some null checking included.

HttpNotFoundResult and HttpUnauthorizedResult

These two results are actually just implementing the HttpStatusCodeResult, which means that they are very simple and just set the StatusCode to 404 for the HttpNotFoundResult and 401 for the HttpUnauthorizedResult.

JavaScriptResult

About as simple as plenty of the others, this is just a quick way of getting JavaScript returned from a action. It’s similar to the ContentResult, but it has the ContentType hardcoded to “application/x-javascript” and just writes out the Script property.

public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}

HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "application/x-javascript";

if (Script != null)
{
response.Write(Script);
}
}

JsonResult

This one is a bit more complex, but still not very. It also has hardcoded its ContentType, but what makes it a bit more complex is that it uses a hardcoded JavaScriptSerializer to serialize the JSON data before writing it directly to the response.

public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (JsonRequestBehavior == JsonRequestBehavior.DenyGet &&
String.Equals(context.HttpContext.Request.HttpMethod,
"GET", StringComparison.OrdinalIgnoreCase))
{
throw new InvalidOperationException(
MvcResources.JsonRequest_GetNotAllowed);
}

HttpResponseBase response = context.HttpContext.Response;

if (!String.IsNullOrEmpty(ContentType)) {
response.ContentType = ContentType;
}
else {
response.ContentType = "application/json";
}
if (ContentEncoding != null) {
response.ContentEncoding = ContentEncoding;
}
if (Data != null) {
JavaScriptSerializer serializer =
new JavaScriptSerializer();
response.Write(serializer.Serialize(Data));
}
}

RedirectResult and RedirectToRouteResult

These to are a little bit more complex, but both are ways of redirecting. Each one can either be a permanent or temporary redirect and they both just use the Redirect methods on the Response object.

For redirecting to a route, it is going to generate a URL to the route using the UrlHelper’s GenerateUrl method. For the RedirectResult it is instead going to use the UrlHelpers GenerateContentUrl method.

Either of these two are useful, and both will maintain your TempData if you need to pass something along with the redirect, all you have to do is put it in TempData.

Conclusion

I hope you’ve learned that the results of actions in MVC are not actually very complicated, but there is a lot you can do with them. You’re not being forced into just displaying views. You have a lot more control than that. None of them were that complicated were they? The code under the hood is not always complicated, so it’s worth taking a look from time to time. If you examine how something is working, it’s often far easier to use it.

Comments (3) -

  • ashok

    11/1/2011 5:18:43 AM | Reply

    Very interesting.. thanks for the post

  • Ajay

    1/31/2012 7:24:00 AM | Reply

    goodd

  • Edward

    9/15/2014 7:00:56 PM | Reply

    I think this article would have been more helpful if the code examples had been spent on how these different results were returned in your controller rather than the underlying code for the inherited class.  I've now read the article but am no wiser in how to return contentresult from a controller action to an ajax call.

Loading