Tuesday 26 January 2016

What is the relationship between TestController and Test?

What is the relationship between TestController and Test?

TestController is the class name whereas Test is the controller name.Please note, when you type the controller name on the URL it should be without the word controller.
Asp.Net MVC follows Convention based approach. It strictly look’s into the conventions we used.
In Asp.Net MVC two things are very important.
  1. How we name something?
  2. Where we keep something?

What is Action Method?

Action method is simply a public method inside controller which accepts user’s request and returns some response. In above example, action method “GetString” is returning a string response type.
Note: In Asp.Net Web Forms default return response is always HTML. In case we want to return something other than HTML (in asp.net Web Forms), we create HTTP handlers, override content type , do response.end etc. It’s not an easy task. In Asp.net MVC it’s very easy. If return type is ‘string’ you can just return string Smile | :) , you do not need to send complete HTML.

What will happen if we try to return an object from an action method?

Look at the following code block.
namespace WebApplication1.Controllers
{
    public class Customer
    {
        public string CustomerName { get; set; }
        public string Address { get; set; }
    }
    public class TestController : Controller
    {
        public Customer GetCustomer()
        {
            Customer c = new Customer();
            c.CustomerName = "Customer 1";
            c.Address = "Address1";
            return c;
        }
    }
}    
Output of above action method will look as shown below.

When return type is some object like ‘customer’, it will return ‘ToString()’ implementation of that object.By default ‘ToString()’ method returns fully qualified name of the class which is “NameSpace.ClassName”;

What if you want to get values of properties in above example?

Simply override “ToString” method of class as follows.
public override string ToString()
{
     return this.CustomerName+"|"+this.Address;
}
Press F5. Output will be as follows.

Is it must to decorate action methods with public access modifier?

Yes, every public method will become action methods automatically.

What about non-public methods?

They are simply methods of a class and not available publicly . In simple words these methods can not be invoked from the web.

What if we want a method to be public but not action method?

Simply decorate it with NonAction attribute as follows.
[NonAction]
public string SimpleMethod()
{
    return "Hi, I am not action method";
}
When we try to make request to above action method we will get following response.

Understand Views in Asp.Net MVC

As we discussed earlier controller will handle the user’s requests and send the response. Most commonly the response is HTML as browser understands that format much better. HTML with some images, texts, Input controls etc. Normally in technical world layer defining the user interface design is termed as UI layer and in Asp.Net MVC it is termed as View.

Lab 2 – Demonstrating Views

In the first lab we created a simple MVC application with just controller and simple string return type. Let us go add view part to the MVC application.
Step 1 – Create new action method
Add a new action method inside TestController as follows.
public ActionResult GetView()
{
    return View("MyView");
}
Step 2 – Create View
Step 2.1. Right click the above action method and select “Add View”.

Step 2.2. In the “Add View” dialog box put view name as “MyView”, uncheck “use a layout” checkbox and click “Add”.

It will add a new view inside “Views/Test” folder in solution explored
Step 3 – Add contents to View
Open MyView.cshtml file and add contents as follows.
@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>MyView</title>
</head>
<body>
Welcome to MVC 5 Step by Step learning
</body> </html>
Step 3. Test and Execute
Press F5 and execute the application.

No comments:

Post a Comment

SqlDataBaseLibrary

using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using AOS.Repository.Infrastructure; using S...