Part 144 How to check if the request method is a GET or a POST in MVC

  • دعنا نشارك هذا :
ترتيب الدرس : 144
المدة : 00:05:59
التحكم :
Text version of the video
http://csharp-video-tutorials.blogspot.com/2013/09/part-144-how-to-check-if-request-method.html

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Slides
http://csharp-video-tutorials.blogspot.com/2013/11/part-144-how-to-check-if-request-method.html

All ASP .NET Text Articles
http://csharp-video-tutorials.blogspot.com/p/free-aspnet-video-tutorial.html

All ASP .NET Slides
http://csharp-video-tutorials.blogspot.com/p/aspnet-slides.html

ASP.NET Playlist
https://www.youtube.com/playlist?listPL4cyC4G0M1RQcB4IYS_zwkyBwMyx5AnDM

All Dot Net and SQL Server Tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

All Dot Net and SQL Server Tutorials in Arabic
https://www.youtube.com/c/KudvenkatArabic/playlists

In asp.net webforms, IsPostBack page property is used to check if the request is a GET or a POST request. If IsPostBack property returns true, then the request is POST, else the request is GET.

In asp.net mvc, use Request object's HttpMethod property to check if the request is a GET or a POST request. Let's discuss using Request.HttpMethod with an example.
1. Create a new asp.net mvc application and name MVCDemo.
2. Add HomeController using "Empty MVC controller" scaffolding template
3. Copy and paste the following code
public class HomeController : Controller
{
// Action method that responds to the GET request
[HttpGet]
public ActionResult Index()
{
ViewBag.IsPostBackIsPostBack();
return View();
}

// Action method that responds to the POST request
[HttpPost]
[ActionName("Index")]
public ActionResult Index_Post()
{
ViewBag.IsPostBackIsPostBack();
return View();
}

// This method checks if a request is a GET or a POST request
private bool IsPostBack()
{
return Request.HttpMethod"POST";
}
}

4. Right click on the Index() action method in HomeController and select "Add View" from the context menu. Set
View nameIndex
View engineRazor
Create a strongly typed viewunchecked
Create a partial viewunchecked
Use a layout or master pageunchecked
Click Add.

5. Copy and paste the following code in Index.cshtml view.
[h3 style"font-family:Arial"]
[email protected]
[/h3]

@using (@Html.BeginForm())
{
[input type"submit" value"Submit" /]
}

Build the project and navigate to http://localhost/MVCDemo/Home/Index. Notice that when you first visit the Page, a GET request is issued and hence IsPostBackFalse. Now click Submit button and notice that IsPostBackTrue is displayed, as a POST request is issued upon clicking the Submit button.

سيتم تفعيل التعليقات قريبا

y
Sep 23, 2020 07:32:26
youtube