1. Query String Parameters : You can receive values from the query string in the URL.

     Request URL : /Home/Index?name=John

    public IActionResult Index(string name) {
       
    }

2. Route Parameters : You can use parameters defined in the route template.

    Request URL : /Home/Index/1

    [Route("Home/Index/{id}")]
    public IActionResult Index(int id) {

    }

3. Form Data : You can receive form data from HTTP POST requests.

    Request Body(form data) : name = john

    [HttpPost]
    public IActionResult Index([FromForm] string name)

4. Request Body : You can receive the HTTP request body. This is mainly used to receive JSON data.

    Request Body(Json): {"Name":"John","Age":30}
    public class User {
        public string Name { get; set; }
        public int Age { get; set; }

    }

    [HttpPost]
    public IActionResult Index([FromBody] User user) {
    }

  5. Route Data : You can receive values from route data. 

    Request URL : /Home/Index/1

    public IActionResult Index() {
        var id = RouteData.Values["id"];
    }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");

                routes.MapRoute("DefaultApiWithId", "Api/{controller}/{id}", new { id = RouteParameter.Optional }, new { id = @"\d+" });
                routes.MapRoute("DefaultApiWithAction", "Api/{controller}/{action}");
                routes.MapRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Get) });
                routes.MapRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" }, new { httpMethod = new HttpMethodConstraint(HttpMethod.Post) });

            });
        }



WebApplication3.zip


I will continue to explain the previous course.

So you need to download source of the previous course.


Add DbContext to Startup.cs file.



Select Mvc Controller with views, using Entity Framework  on the select controller type screen.



At the next dialog input as follow.



Now we have to link the Customers controller.

Open Views/Shared/_Layout.cshtml and add follow lines.


All of the work is finish.


Run the program. Press F5.


select Customers.


You can see the customers list page.

Naviage the site and add, modify and delete customer.




Thank you.

WebApplication3.zip


If you want to practice this lecture, You have to install SqlServer first.


In this lecture, We will practice how to convert tables that is in the sqlserver database to MVC models .


First, Open VisualStudio2017 and create view project.


Press 확인.


And then select Web Mvc Application.


After that the project is created.


Second, Connect to Database.

Click Server Explorer that is located left bookmark or enter shortcut(ctrl + alt + S).



Press mouse right button at the Connect to Database tree item.

And then click Add new connection.

Add new connection popup will be displayed then input "." at the server name and choose database name what you want to use.

Press Ok button then new database is displayed at the server explorer tab.

if you select what you just added and press F4,then property window will be displayed.


There is a Connection string item which is connection info of the c#.

Copy it and paste in the starup.cs file as follow.




kgchoiContext that is marked as a red wave underline is composite of the Database Name + "Context".

So you have change the kgchoi string to your database name and below script too.


After that input follow script at the package management console and press enter.


Scaffold-DbContext "Server=.;Database=kgchoi;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models -Force



If the script ended successfully, you will see the add new models.


Customers is a table model and kgchoiContext is a mapping script between database table and model.


Now We made models that is mapping to table.


At the next time I will make a simple controller using the Customers model.

+ Recent posts