How to pass parameters to action methods in ASP.NET Core MVC

ASP.Internet Core is a cross-system, open up supply, lean, speedy, and modular framework for making high-effectiveness website apps. There are a range of methods in which you can pass parameters to motion methods in ASP.Internet Core MVC. You can pass them via a URL, a question string, a request header, […]

ASP.Internet Core is a cross-system, open up supply, lean, speedy, and modular framework for making high-effectiveness website apps. There are a range of methods in which you can pass parameters to motion methods in ASP.Internet Core MVC. You can pass them via a URL, a question string, a request header, a request human body, or even a sort. This post talks about all of these methods, and illustrates them with code illustrations.

To perform with the code illustrations offered in this post, you should really have Visible Studio 2019 set up in your process. If you don’t currently have a copy, you can down load Visible Studio 2019 here. 

Generate an ASP.Internet Core MVC job in Visible Studio 2019

To start with off, let us make an ASP.Internet Core job in Visible Studio 2019. Assuming Visible Studio 2019 is set up in your process, adhere to the ways outlined under to make a new ASP.Internet Core job in Visible Studio.

  1. Start the Visible Studio IDE.
  2. Click on on “Create new job.”
  3. In the “Create new project” window, select “ASP.Internet Core Net Application” from the record of templates shown.
  4. Click on Following.
  5. In the “Configure your new project” window, specify the title and locale for the new job.
  6. Optionally test the “Place answer and job in the very same directory” test box, depending on your tastes.
  7. Click on Generate.
  8. In the “Create a New ASP.Internet Core Net Application” window shown following, select .Internet Core as the runtime and ASP.Internet Core 3.1 (or later) from the fall-down record at the top.
  9. Select “Web Application (Product-Perspective-Controller)” as the job template to make a new ASP.Internet Core MVC software. 
  10. Make certain that the test boxes “Enable Docker Support” and “Configure for HTTPS” are unchecked as we will not be using these capabilities here.
  11. Make certain that Authentication is established to “No Authentication” as we will not be using authentication both.
  12. Click on Generate.

Pursuing these ways should really make a new ASP.Internet Core MVC job in Visible Studio 2019. We’ll use this job in the sections under to illustrate the a variety of methods of passing parameters to motion methods in ASP.Internet Core 3.1.

Generate an AuthorRepository class in ASP.Internet Core MVC

In this instance we’ll be using a repository class — the motion methods in the controller will interact with the methods of the repository class for CRUD operations. We’ll very first make a product class named Creator with minimum properties for the sake of simplicity as shown in the code snippet presented under.

    community class Creator
   
        community int Id get established
        community string FirstName get established
        community string LastName get established
   

The AuthorRepository class contains methods for retrieving situations of the Creator class from a generic record as effectively as for including new situations of the Creator class to the generic record. The GetAuthors approach returns a web page of information, the web page range becoming passed to it as an argument.

    community class AuthorRepository
    {
        Checklist authors = new Checklist()
       
            new Creator
           
                Id = 1,
                FirstName = "Joydip",
                LastName = "Kanjilal"
            ,
            new Creator
           
                Id = 2,
                FirstName = "Steve",
                LastName = "Smith"
           
       
        community Creator GetAuthor(int id)
       
            return authors.FirstOrDefault(a => a.Id == id)
       
        community Checklist GetAuthors(int pageNumber = 1)
       
            int pageSize = 10
            int skip = pageSize * (pageNumber - 1)
            if (authors.Depend < pageSize)
                pageSize = authors.Depend
            return authors
              .Skip(skip)
              .Just take(pageSize).ToList()
       
        community bool Help save(Creator author)
       
            var final result = authors.In which(a => a.Id == author.Id)
            if (final result != null)
            
                if (final result.Depend() == )
               
                    authors.Increase(author)
                    return real
               
           
            return untrue
       
    }

Pass parameters via the URL in ASP.Internet Core MVC

One particular of the easiest and most straightforward methods to pass parameters to an motion approach is passing it via the URL. The adhering to code snippet illustrates how you can pass parameters in the URL.

[HttpGet]
[Route("Default/GetAuthor/authorId:int")]
community IActionResult GetAuthor(int authorId)

   var information = authorRepository.GetAuthor(authorId)
   return Perspective(information)

The URL to the endpoint is:

Copyright © 2020 IDG Communications, Inc.

Rosa G. Rose

Next Post

Multicloud is not really about clouds anymore

Mon Jul 27 , 2020
Most think of multicloud just how it appears: an architecture that leverages plural general public and/or non-public clouds at the exact time, in assistance of greatest-of-breed cloud products and services. In other words, we use multicloud as a route to accessibility the cloud products and services that are the greatest […]