Core MVC Pass-value request and HttpContext

Source: Internet
Author: User
Tags form post httpcontext

1. Method of passing values

Method of using Request (1-3):

1) Query: Get links? The values that follow

such as: Http://localhost:55842/Home/About?name=kxy

 public   Iactionresult about () {Vie wdata[  " message  " ] =  " your application Description page.   "            ;  // -------------------------Request--------------- ----------------- string  name = Request.query["  name   "];//  Get a connection? name=kxy  return          Content (name); }

Result: page display Kxy

2) Form: Get the value of form post

string name = request.form["name"];

3) Cookies: Locally saved values

string name1 = request.cookies["name"];

Methods for using HttpContext (4):

4) Session: Value Saved by server

usingSystem.Diagnostics;usingMICROSOFT.ASPNETCORE.MVC;usingCoremvc.models;usingMicrosoft.AspNetCore.Http;namespacecoremvc.controllers{ Public classHomecontroller:controller { PublicIactionresult Index () {returnView (); }         PublicIactionresult About () {//-------------------------HttpContext----------------------------//--------------------------Session-------------------------------//need HttpContext to reference using Microsoft.AspNetCore.Http;HttpContext.Session.SetString ("name","Kxy"); stringName = HttpContext.Session.GetString ("name"); HttpContext.Session.Remove ("name"); HttpContext.Session.SetInt32 (" Age", A); int? Age = HttpContext.Session.GetInt32 (" Age"); returnContent (name); }    }}

Also, you need to modify the Startup.cs

The Configureservices method injects (that is, adds)            services. Addsession (); The Configure method adds ( tells ASP. NET core uses memory to store session data )            app. Usesession ();

Core MVC Pass-value request and HttpContext

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.