7 days of WordPress plugins, themes & templates - for free!* Unlimited asset downloads! Start 7-Day Free Trial

Next lesson playing in 5 seconds

Cancel
  • Overview
  • Transcript

3.1 Handling GET Requests

An ASP.NET MVC application is all about handling requests. In this lesson, I'll show you how to handle GET requests.

3.1 Handling GET Requests

In the previous lesson you learned just a little bit about how an MVC application handles requests. And you learned that what we see in the browser, or really what's returned to a client is typically the result of executing a method on a controller, and in this lesson, we are going to put that into practice. We are going to write a method that is going to display a form, so that we can fill it out and then create a message. And then we are going to write another method that is going to accept that data, that we put into the form, so that we can ultimately store that in our data store. So, the first thing we want to do is write the method that is going to display our form. This is a get request, basically. So, we want this method to be public, and we also want to return some type of action result. Now for our return type, we want it to be as generic as possible. And I will show you why here in a few moments, because we actually have a variety of different action results. So, let's call this simply Create, because that is what we are doing, and this is going to create a resource on our website. In order to access this resource, we would go to the URL, /message/create. So, it's very correct to think of an action method as a resource within our application. Now, if you're not familiar with the idea of a resource, just think of what URL stands for, it stands for Universal Resource Locator. It's a way that we can locate resources on a remote system, or it doesn't have to be a remote system, it could be actually on our local machine, but usually, it's a remote system and usually, it's a website. In typical websites and applications, resources are files on the file system. And in an MVC application, that is somewhat true, because we still have static files, like CSS, JavaScript images, and things like that. But, we also have these action methods, and these are essentially resources within our application. So, whenever we finish writing this method we will be able to run it and we can go to message/create and we could see the result of executing this Create method. So, we are eventually going to simply just return the view. Because this is a get request we don't need to process anything, we are just displaying a simple form, but let's look at a few other action results. Now, the view method returns a view result, that makes sense. We also have a Json method that returns a Json result, but then we can also return just plain text, and we have a Content method for that. So, let's just say hello create method, and then we could run this, and whenever we go to /message/create, then we will see that message in the browser. But I have an error here, and I did not type a semicolon there, so now we can run the application. I'm using Ctrl-F5, so that we can run the application but we can also edit it as well. And if we just go to message/create we see our message, hello create method. Now, let's go back to our code and let's change this Create method so that it returns a view. Later on, whenever we start adding web API features, we are going to return other types of results, like for retrieving a list of messages. Well, we could return that as HTML, but if we were making the request with an XML HTTP object through JavaScript, then we could return a Json structure. So, there are different things that we can do as far as returning different types of results within the same method. That's called content negotiation and we will cover that later on. So, for right now, we're just going to return a view, but we don't have a view for the Create method. In fact, if we go back to the browser and refresh, instead of seeing our content, now we have the content of the error page. And basically what this would say is that we don't have a view for this action method. In fact, right here, the view Create was not found. And in the previous lesson, you learned where the MVC application is looking for that view. It's looking inside of the Views folder, and since this is the message controller, it's looking for a folder called Message. Now, we don't have to have a message folder in here, because it will also look inside of this shared folder, and if it finds a view there called Create, then it will return that, but it's not there. So, let's create a view. Now, we can right click inside of our action method and there should be an option to create a view. However, I am not seeing that here. That was there in prior versions of Visual Studio, but it apparently is not there anymore. Now, this is still beta software, so that could be a feature that is going to be added in. I can't imagine it not being here now, but you never know. So, we are going to do this the hard way. We're going to go to the Views folder, and we are going to add a new folder simply called Message, and then we are going to create a view called Create. So, we want to add a new item, and somewhere around in here we will have a view. So, let's do a search for view, and there we go, we have an MVC view page. And let's just call this Create. Now, the view that's created is rather void of just about anything. At the very top we have a comment, that says for more information on enabling MVC for empty projects and blah, blah, blah, blah, blah, we can get rid of that, but we also have a code block where the code for setting the title of the page is commented out. Now, let's first of all talk about this @ sign. The @ sign is very special with the Razor view engine because that's what toggles into code mode if you will, and for example, the @ sign followed by a * or an asterisk is a comment. So, anything inside of the @*, and then *@, that's difficult to say, is going to be a comment. So, this is a multi-line comment, we can add more lines here, and we can just say blah, blah, blah, and all of these are still comments. So, let's just get rid of that. Now the @ sign followed by an open curly brace is a code block. So, just about anything inside of this Razor code block, that's what we call it, is going to be C# code. However, we can also mix some HTML, so if we wanted to add something here like a P element, and then have content, we could do that. The Razor view engine is smart enough to know that this line is C# code, this line is HTML, and that's very useful whenever you need to iterate over a collection, or if you have an if statement, or things like that. We will be getting into those types of things later on. For now, we're just going to keep things simple, and we're going to set the title of the page to let's say, Create a Message. Now, we should also talk about this view bag, this is a special object. It's dynamic, and you can use it to pass information between the controller and the view, and you don't want to do that all the time. In fact you want to do that very sparingly, because there are better ways of getting data into the view. But in the case here, we aren't sending data in between the controller and the view. Instead, we are setting the title inside of what is essentially the content page so that it can be used inside of the layout page. And let's go ahead and look at the layout page, it's inside of the Shared folder. Now, the Shared folder is shared between all of the controllers and the views, so this is where we put anything that could be used in any controller and in any view, and in the case of the layout page, it's _Layout.cshtml. Now by convention, the files that begin with an underscore are support files, they typically aren't called directly by a controller. So, contrast that with a view, like the Create view that we just made. We didn't explicitly say, hey use the Create view, but we implied that by not telling the view engine what view that we wanted to use, because we could pass a string here that would be the name of the view that we wanted to use. In this case, we would use the foo View, but we didn't, so the View engine is going to imply that we want to use the Create view. So the Create view is directly called from a controller, the underscore layouts and you can see a few other views that begin with an underscore, are not. Now you can see this _LoginPartial. That's a partial view, that's a smaller view that we can reuse in our other views, and we're going to go over all this in more detail later on. So, let's briefly look at the layout page and right here for the title element, we can see @ViewBag.Title. Now, we've talked about Razor code blocks and Razor comments, this is a Razor expression. A Razor expression is used to essentially inject a value into the HTML response. So, we say title is going to have whatever we set as the ViewBag.title, and then you can see that it also has the AppSettings.Options.SiteTitle. So, the title will be whatever we set inside of the view, in this case create a message, and then I have a feeling that the site title is going to be sound board. Whenever we run it we'll just have to see if that is indeed the case. Okay, so, we have our very simple view here, but let's add some content. Let's have an H3 elements, or no let's make this an H2, that says Create a Message, or not Create a Message, let's do Post a Message because that's kind of what we're doing. Let's also change the title there, and then we want just a very simple form. Now, there are some HTML helpers that help us generate HTML, and we will talk about those later on. So, for right now, we're just going to use normal everyday HTML. So, let's set the action equal to /message/, and let's see, this is going to go to the create, and this is also going to be a post method. So, let's change that to post, and then we just need a few fields. Now, we need a title, we need the message and then we need a button to submit. So, we could trick this out with labels and stuff, but for right now, let's just have our input elements. Let's set the type="text", and then let's give it a name of, let's call it "messageTitle", and we also need the id set to the same thing, "messageTitle", and we won't have anything for the value, and we do need to close this tag. So, let's copy and paste that. This other one is going to be message content, and we will just copy that and paste it. Let's put this on another line and then let's have our submit button. So, type equals submit, and let's set the value to submit, and we are going to come back to this and make some improvements here, but for right now, let's just go ahead and run our application. I'm going to do Ctrl-F5, and then we will just navigate to /message and then create. So, ideally, we would add a link here so that we can just click on that link and then go to message create, but now we have our forum. We can post a message, and let's see what the title is, yes, SoundBoard, so that is indeed the case. So, let's do title and then content and then let's submit, and it looks like that nothing happened, but something did indeed happen. We sent a post request to /message/create that was handled by the Create action method on the message controller, and all it did is send back the view. There was no processing of that data because the code that we have currently written doesn't do anything with that data. So, the next thing we need to do is write a method to handle post requests. And I had originally planned to do that in this lesson, but it looks like that we will do that in the next one.

Back to the top