Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Warning
This version of ASP.NET Core is no longer supported. For more information, see the .NET and .NET Core Support Policy. For the current release, see the .NET 9 version of this article.
Important
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps.
MVC-based apps contain:
Movie
model retrieves movie data from a database, provides it to the view or updates it. Updated data is written to a database.In an MVC app, the view only displays information. The controller handles and responds to user input and interaction. For example, the controller handles URL segments and query-string values, and passes these values to the model. The model might use these values to query the database. For example:
https://localhost:5001/Home/Privacy
: specifies the Home
controller and the Privacy
action.https://localhost:5001/Movies/Edit/5
: is a request to edit the movie with ID=5 using the Movies
controller and the Edit
action, which are detailed later in the tutorial.Route data is explained later in the tutorial.
The MVC architectural pattern separates an app into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps manage complexity when building an app, because it enables work on one aspect of the implementation at a time without impacting the code of another. For example, you can work on the view code without depending on the business logic code.
These concepts are introduced and demonstrated in this tutorial series while building a movie app. The MVC project contains folders for the Controllers and Views.
In Solution Explorer, right-click Controllers > Add > Controller.
In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.
In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs
and select Add.
Replace the contents of Controllers/HelloWorldController.cs
with the following code:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers;
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my default action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Every public
method in a controller is callable as an HTTP endpoint. In the sample above, both methods return a string. Note the comments preceding each method.
An HTTP endpoint:
https://localhost:5001/HelloWorld
.HTTPS
.localhost:5001
.HelloWorld
.The first comment states this is an HTTP GET method that's invoked by appending /HelloWorld/
to the base URL.
The second comment specifies an HTTP GET method that's invoked by appending /HelloWorld/Welcome/
to the URL. Later on in the tutorial, the scaffolding engine is used to generate HTTP POST
methods, which update data.
Run the app without the debugger by pressing Ctrl+F5.
Append /HelloWorld
to the path in the address bar. The Index
method returns a string.
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The routing format is set in the Program.cs
file.
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:
localhost:5001/HelloWorld
maps to the HelloWorld Controller class.localhost:5001/HelloWorld/Index
causes the Index
method of the HelloWorldController
class to run. Notice that you only had to browse to localhost:5001/HelloWorld
and the Index
method was called by default. Index
is the default method that will be called on a controller if a method name isn't explicitly specified.id
) is for route data. Route data is explained later in the tutorial.Browse to: https://localhost:{PORT}/HelloWorld/Welcome
. Replace {PORT}
with your port number.
The Welcome
method runs and returns the string This is the Welcome action method...
. For this URL, the controller is HelloWorld
and Welcome
is the action method. You haven't used the [Parameters]
part of the URL yet.
Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4
.
Change the Welcome
method to include two parameters as shown in the following code.
// GET: /HelloWorld/Welcome/
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}
The preceding code:
numTimes
parameter defaults to 1 if no value is passed for that parameter.HtmlEncoder.Default.Encode
to protect the app from malicious input, such as through JavaScript.$"Hello {name}, NumTimes is: {numTimes}"
.Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4
. Replace {PORT}
with your port number.
Try different values for name
and numtimes
in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information.
In the previous image:
Parameters
isn't used.name
and numTimes
parameters are passed in the query string.?
(question mark) in the above URL is a separator, and the query string follows.&
character separates field-value pairs.Replace the Welcome
method with the following code:
public string Welcome(string name, int ID = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}
Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick
In the preceding URL:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
starts the query string.app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
In the preceding example:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
(in id?
) indicates the id
parameter is optional.The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps.
MVC-based apps contain:
Movie
model retrieves movie data from a database, provides it to the view or updates it. Updated data is written to a database.In an MVC app, the view only displays information. The controller handles and responds to user input and interaction. For example, the controller handles URL segments and query-string values, and passes these values to the model. The model might use these values to query the database. For example:
https://localhost:5001/Home/Privacy
: specifies the Home
controller and the Privacy
action.https://localhost:5001/Movies/Edit/5
: is a request to edit the movie with ID=5 using the Movies
controller and the Edit
action, which are detailed later in the tutorial.Route data is explained later in the tutorial.
The MVC architectural pattern separates an app into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps manage complexity when building an app, because it enables work on one aspect of the implementation at a time without impacting the code of another. For example, you can work on the view code without depending on the business logic code.
These concepts are introduced and demonstrated in this tutorial series while building a movie app. The MVC project contains folders for the Controllers and Views.
In Solution Explorer, right-click Controllers > Add > Controller.
In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.
In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs
and select Add.
Replace the contents of Controllers/HelloWorldController.cs
with the following code:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers;
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my default action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Every public
method in a controller is callable as an HTTP endpoint. In the sample above, both methods return a string. Note the comments preceding each method.
An HTTP endpoint:
https://localhost:5001/HelloWorld
.HTTPS
.localhost:5001
.HelloWorld
.The first comment states this is an HTTP GET method that's invoked by appending /HelloWorld/
to the base URL.
The second comment specifies an HTTP GET method that's invoked by appending /HelloWorld/Welcome/
to the URL. Later on in the tutorial, the scaffolding engine is used to generate HTTP POST
methods, which update data.
Run the app without the debugger by pressing Ctrl+F5.
Append /HelloWorld
to the path in the address bar. The Index
method returns a string.
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The routing format is set in the Program.cs
file.
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:
localhost:5001/HelloWorld
maps to the HelloWorld Controller class.localhost:5001/HelloWorld/Index
causes the Index
method of the HelloWorldController
class to run. Notice that you only had to browse to localhost:5001/HelloWorld
and the Index
method was called by default. Index
is the default method that will be called on a controller if a method name isn't explicitly specified.id
) is for route data. Route data is explained later in the tutorial.Browse to: https://localhost:{PORT}/HelloWorld/Welcome
. Replace {PORT}
with your port number.
The Welcome
method runs and returns the string This is the Welcome action method...
. For this URL, the controller is HelloWorld
and Welcome
is the action method. You haven't used the [Parameters]
part of the URL yet.
Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4
.
Change the Welcome
method to include two parameters as shown in the following code.
// GET: /HelloWorld/Welcome/
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}
The preceding code:
numTimes
parameter defaults to 1 if no value is passed for that parameter.HtmlEncoder.Default.Encode
to protect the app from malicious input, such as through JavaScript.$"Hello {name}, NumTimes is: {numTimes}"
.Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4
. Replace {PORT}
with your port number.
Try different values for name
and numtimes
in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information.
In the previous image:
Parameters
isn't used.name
and numTimes
parameters are passed in the query string.?
(question mark) in the above URL is a separator, and the query string follows.&
character separates field-value pairs.Replace the Welcome
method with the following code:
public string Welcome(string name, int ID = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}
Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick
In the preceding URL:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
starts the query string.app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
In the preceding example:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
(in id?
) indicates the id
parameter is optional.The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps.
MVC-based apps contain:
Movie
model retrieves movie data from a database, provides it to the view or updates it. Updated data is written to a database.In an MVC app, the view only displays information. The controller handles and responds to user input and interaction. For example, the controller handles URL segments and query-string values, and passes these values to the model. The model might use these values to query the database. For example:
https://localhost:5001/Home/Privacy
: specifies the Home
controller and the Privacy
action.https://localhost:5001/Movies/Edit/5
: is a request to edit the movie with ID=5 using the Movies
controller and the Edit
action, which are detailed later in the tutorial.Route data is explained later in the tutorial.
The MVC architectural pattern separates an app into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps manage complexity when building an app, because it enables work on one aspect of the implementation at a time without impacting the code of another. For example, you can work on the view code without depending on the business logic code.
These concepts are introduced and demonstrated in this tutorial series while building a movie app. The MVC project contains folders for the Controllers and Views.
In Solution Explorer, right-click Controllers > Add > Controller.
In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.
In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs
and select Add.
Replace the contents of Controllers/HelloWorldController.cs
with the following code:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers;
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my default action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
Every public
method in a controller is callable as an HTTP endpoint. In the sample above, both methods return a string. Note the comments preceding each method.
An HTTP endpoint:
https://localhost:5001/HelloWorld
.HTTPS
.localhost:5001
.HelloWorld
.The first comment states this is an HTTP GET method that's invoked by appending /HelloWorld/
to the base URL.
The second comment specifies an HTTP GET method that's invoked by appending /HelloWorld/Welcome/
to the URL. Later on in the tutorial, the scaffolding engine is used to generate HTTP POST
methods, which update data.
Run the app without the debugger by pressing Ctrl+F5.
Append /HelloWorld
to the path in the address bar. The Index
method returns a string.
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The routing format is set in the Program.cs
file.
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:
localhost:5001/HelloWorld
maps to the HelloWorld Controller class.localhost:5001/HelloWorld/Index
causes the Index
method of the HelloWorldController
class to run. Notice that you only had to browse to localhost:5001/HelloWorld
and the Index
method was called by default. Index
is the default method that will be called on a controller if a method name isn't explicitly specified.id
) is for route data. Route data is explained later in the tutorial.Browse to: https://localhost:{PORT}/HelloWorld/Welcome
. Replace {PORT}
with your port number.
The Welcome
method runs and returns the string This is the Welcome action method...
. For this URL, the controller is HelloWorld
and Welcome
is the action method. You haven't used the [Parameters]
part of the URL yet.
Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4
.
Change the Welcome
method to include two parameters as shown in the following code.
// GET: /HelloWorld/Welcome/
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}
The preceding code:
numTimes
parameter defaults to 1 if no value is passed for that parameter.HtmlEncoder.Default.Encode
to protect the app from malicious input, such as through JavaScript.$"Hello {name}, NumTimes is: {numTimes}"
.Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4
. Replace {PORT}
with your port number.
Try different values for name
and numtimes
in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information.
In the previous image:
Parameters
isn't used.name
and numTimes
parameters are passed in the query string.?
(question mark) in the above URL is a separator, and the query string follows.&
character separates field-value pairs.Replace the Welcome
method with the following code:
public string Welcome(string name, int ID = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}
Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick
In the preceding URL:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
starts the query string.app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
In the preceding example:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
(in id?
) indicates the id
parameter is optional.The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps.
MVC-based apps contain:
Movie
model retrieves movie data from a database, provides it to the view or updates it. Updated data is written to a database.In an MVC app, the view only displays information. The controller handles and responds to user input and interaction. For example, the controller handles URL segments and query-string values, and passes these values to the model. The model might use these values to query the database. For example:
https://localhost:5001/Home/Privacy
: specifies the Home
controller and the Privacy
action.https://localhost:5001/Movies/Edit/5
: is a request to edit the movie with ID=5 using the Movies
controller and the Edit
action, which are detailed later in the tutorial.Route data is explained later in the tutorial.
The MVC architectural pattern separates an app into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps manage complexity when building an app, because it enables work on one aspect of the implementation at a time without impacting the code of another. For example, you can work on the view code without depending on the business logic code.
These concepts are introduced and demonstrated in this tutorial series while building a movie app. The MVC project contains folders for the Controllers and Views.
In Solution Explorer, right-click Controllers > Add > Controller.
In the Add New Scaffolded Item dialog box, select MVC Controller - Empty > Add.
In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs
and select Add.
Replace the contents of Controllers/HelloWorldController.cs
with the following code:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my default action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
}
Every public
method in a controller is callable as an HTTP endpoint. In the sample above, both methods return a string. Note the comments preceding each method.
An HTTP endpoint:
https://localhost:5001/HelloWorld
.HTTPS
.localhost:5001
.HelloWorld
.The first comment states this is an HTTP GET method that's invoked by appending /HelloWorld/
to the base URL.
The second comment specifies an HTTP GET method that's invoked by appending /HelloWorld/Welcome/
to the URL. Later on in the tutorial, the scaffolding engine is used to generate HTTP POST
methods, which update data.
Run the app without the debugger.
Append "HelloWorld" to the path in the address bar. The Index
method returns a string.
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The routing format is set in the Program.cs
file.
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:
localhost:5001/HelloWorld
maps to the HelloWorld Controller class.localhost:5001/HelloWorld/Index
causes the Index
method of the HelloWorldController
class to run. Notice that you only had to browse to localhost:5001/HelloWorld
and the Index
method was called by default. Index
is the default method that will be called on a controller if a method name isn't explicitly specified.id
) is for route data. Route data is explained later in the tutorial.Browse to: https://localhost:{PORT}/HelloWorld/Welcome
. Replace {PORT}
with your port number.
The Welcome
method runs and returns the string This is the Welcome action method...
. For this URL, the controller is HelloWorld
and Welcome
is the action method. You haven't used the [Parameters]
part of the URL yet.
Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4
.
Change the Welcome
method to include two parameters as shown in the following code.
// GET: /HelloWorld/Welcome/
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}
The preceding code:
numTimes
parameter defaults to 1 if no value is passed for that parameter.HtmlEncoder.Default.Encode
to protect the app from malicious input, such as through JavaScript.$"Hello {name}, NumTimes is: {numTimes}"
.Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4
. Replace {PORT}
with your port number.
Try different values for name
and numtimes
in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information.
In the previous image:
Parameters
isn't used.name
and numTimes
parameters are passed in the query string.?
(question mark) in the above URL is a separator, and the query string follows.&
character separates field-value pairs.Replace the Welcome
method with the following code:
public string Welcome(string name, int ID = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}
Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick
In the preceding URL:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
starts the query string.app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
In the preceding example:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
(in id?
) indicates the id
parameter is optional.The Model-View-Controller (MVC) architectural pattern separates an app into three main components: Model, View, and Controller. The MVC pattern helps you create apps that are more testable and easier to update than traditional monolithic apps.
MVC-based apps contain:
Movie
model retrieves movie data from a database, provides it to the view or updates it. Updated data is written to a database.In an MVC app, the view only displays information. The controller handles and responds to user input and interaction. For example, the controller handles URL segments and query-string values, and passes these values to the model. The model might use these values to query the database. For example:
https://localhost:5001/Home/Privacy
: specifies the Home
controller and the Privacy
action.https://localhost:5001/Movies/Edit/5
: is a request to edit the movie with ID=5 using the Movies
controller and the Edit
action, which are detailed later in the tutorial.Route data is explained later in the tutorial.
The MVC architectural pattern separates an app into three main groups of components: Models, Views, and Controllers. This pattern helps to achieve separation of concerns: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model. This separation helps manage complexity when building an app, because it enables work on one aspect of the implementation at a time without impacting the code of another. For example, you can work on the view code without depending on the business logic code.
These concepts are introduced and demonstrated in this tutorial series while building a movie app. The MVC project contains folders for the Controllers and Views.
In the Solution Explorer, right-click Controllers > Add > Controller.
In the Add Scaffold dialog box, select MVC Controller - Empty.
In the Add New Item - MvcMovie dialog, enter HelloWorldController.cs
and select Add.
Replace the contents of Controllers/HelloWorldController.cs
with the following:
using Microsoft.AspNetCore.Mvc;
using System.Text.Encodings.Web;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
//
// GET: /HelloWorld/
public string Index()
{
return "This is my default action...";
}
//
// GET: /HelloWorld/Welcome/
public string Welcome()
{
return "This is the Welcome action method...";
}
}
}
Every public
method in a controller is callable as an HTTP endpoint. In the sample above, both methods return a string. Note the comments preceding each method.
An HTTP endpoint:
https://localhost:5001/HelloWorld
.HTTPS
.localhost:5001
.HelloWorld
.The first comment states this is an HTTP GET method that's invoked by appending /HelloWorld/
to the base URL.
The second comment specifies an HTTP GET method that's invoked by appending /HelloWorld/Welcome/
to the URL. Later on in the tutorial, the scaffolding engine is used to generate HTTP POST
methods, which update data.
Run the app without the debugger.
Append "HelloWorld" to the path in the address bar. The Index
method returns a string.
MVC invokes controller classes, and the action methods within them, depending on the incoming URL. The default URL routing logic used by MVC, uses a format like this to determine what code to invoke:
/[Controller]/[ActionName]/[Parameters]
The routing format is set in the Configure
method in Startup.cs
file.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
When you browse to the app and don't supply any URL segments, it defaults to the "Home" controller and the "Index" method specified in the template line highlighted above. In the preceding URL segments:
localhost:5001/HelloWorld
maps to the HelloWorldController class.localhost:5001/HelloWorld/Index
causes the Index
method of the HelloWorldController
class to run. Notice that you only had to browse to localhost:5001/HelloWorld
and the Index
method was called by default. Index
is the default method that will be called on a controller if a method name isn't explicitly specified.id
) is for route data. Route data is explained later in the tutorial.Browse to: https://localhost:{PORT}/HelloWorld/Welcome
. Replace {PORT}
with your port number.
The Welcome
method runs and returns the string This is the Welcome action method...
. For this URL, the controller is HelloWorld
and Welcome
is the action method. You haven't used the [Parameters]
part of the URL yet.
Modify the code to pass some parameter information from the URL to the controller. For example, /HelloWorld/Welcome?name=Rick&numtimes=4
.
Change the Welcome
method to include two parameters as shown in the following code.
// GET: /HelloWorld/Welcome/
// Requires using System.Text.Encodings.Web;
public string Welcome(string name, int numTimes = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}");
}
The preceding code:
numTimes
parameter defaults to 1 if no value is passed for that parameter.HtmlEncoder.Default.Encode
to protect the app from malicious input, such as through JavaScript.$"Hello {name}, NumTimes is: {numTimes}"
.Run the app and browse to: https://localhost:{PORT}/HelloWorld/Welcome?name=Rick&numtimes=4
. Replace {PORT}
with your port number.
Try different values for name
and numtimes
in the URL. The MVC model binding system automatically maps the named parameters from the query string to parameters in the method. See Model Binding for more information.
In the previous image:
Parameters
isn't used.name
and numTimes
parameters are passed in the query string.?
(question mark) in the above URL is a separator, and the query string follows.&
character separates field-value pairs.Replace the Welcome
method with the following code:
public string Welcome(string name, int ID = 1)
{
return HtmlEncoder.Default.Encode($"Hello {name}, ID: {ID}");
}
Run the app and enter the following URL: https://localhost:{PORT}/HelloWorld/Welcome/3?name=Rick
In the preceding URL:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
starts the query string.app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
In the preceding example:
id
.Welcome
method contains a parameter id
that matched the URL template in the MapControllerRoute
method.?
(in id?
) indicates the id
parameter is optional.ASP.NET Core feedback
ASP.NET Core is an open source project. Select a link to provide feedback:
Events
Power BI DataViz World Championships
Feb 14, 4 PM - Mar 31, 4 PM
With 4 chances to enter, you could win a conference package and make it to the LIVE Grand Finale in Las Vegas
Learn moreTraining
Module
Create a web API with ASP.NET Core controllers - Training
Create a RESTful service with ASP.NET Core controllers that supports create, read, update, and delete (CRUD) operations.
Documentation
Part 4, add a model to an ASP.NET Core MVC app
Part 4 of tutorial series on ASP.NET Core MVC.
Part 3, add a view to an ASP.NET Core MVC app
Part 3 of tutorial series on ASP.NET Core MVC.
Get started with ASP.NET Core MVC
Learn how to get started with ASP.NET Core MVC.