MVC控制器

using System.Web;
using System.Web.Mvc; 
 
namespace MvcMovie.Controllers 
{ 
    public class HelloWorldController : Controller 
    { 
        // 
        // GET: /HelloWorld/ 
 
        public string Index() 
        { 
            return "This is my <b>default</b> action..."; 
        } 
 
        // 
        // GET: /HelloWorld/Welcome/ 
 
        public string Welcome() 
        { 
            return "This is the Welcome action method..."; 
        } 
    } 
}

控制器方法将返回 HTML 的字符串作为一个例子。 HelloWorldController and the first method is named Index. " _mstHash="2516676" _mstChunk="true">控制器被命名为 HelloWorldController ,第一种方法叫做 Index让我们从浏览器调用它。运行应用程序 (按 F5 或 Ctrl + F5)。在浏览器中,将"HelloWorld"追加到地址栏中的路径。 http://localhost:1234/HelloWorld.) " _mstHash="7550028" _mstChunk="true">(例如,在图中,它下面的 http://localhost:1234/HelloWorld。)页面在浏览器中的将看起来像下面的屏幕快照。在上述的方法中,代码直接返回一个字符串。你告诉系统刚回国时一些 HTML,并且说到做到!

ASP.NET MVC 调用不同的控制器类 (和不同的操作方法内他们) 根据传入的 URL。用 ASP.NET MVC 的默认 URL 路由逻辑使用这样的格式来确定哪些代码来调用:

/[Controller]/[ActionName]/[Parameters]

你设置路由在App_Start/RouteConfig.cs文件中的格式。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

当您运行该应用程序并不提供任何 URL 段时,它将默认为"家"控制器和"指数"的操作方法指定的上面的代码中的默认值。

URL 的第一部分确定要执行的控制器类。 /HelloWorld maps to the HelloWorldController class. " _mstHash="2516684" _mstChunk="true">所以 /HelloWorld 映射到 HelloWorldController 类。URL 的第二部分确定的行动方法上要执行的类。 /HelloWorld/Index would cause the Index method of the HelloWorldController class to execute. " _mstHash="5033368" _mstChunk="true">所以 /HelloWorld/Index 会导致 Index 的方法 HelloWorldController 类来执行。 /HelloWorld and the Index method was used by default. " _mstHash="6291710" _mstChunk="true">请注意,我们只有以浏览到 /HelloWorld Index 方法在默认情况下。 is the default method that will be called on a controller if one is not explicitly specified. " _mstHash="7550052" _mstChunk="true">这是因为一种方法称为 Index 是如果不显式指定一个称为控制器的默认方法。URL 段 (Parameters) 第三部分是为路由数据。

浏览到 http://localhost:xxxx,HelloWorld,欢迎光临 Welcome method runs and returns the string "This is the Welcome action method...". " _mstHash="2516686" _mstChunk="true"> Welcome 方法运行并返回字符串"这是值得欢迎的行动方法......"。 /[Controller]/[ActionName]/[Parameters]. " _mstHash="3775029" _mstChunk="true">MVC 的默认映射是 /[Controller]/[ActionName]/[Parameters] HelloWorld and Welcome is the action method. " _mstHash="5033372" _mstChunk="true">为此 URL,控制器是 HelloWorld Welcome 是操作方法。 [Parameters] part of the URL yet." _mstHash="6291715" _mstChunk="true">你还没有使用 [Parameters] 尚未的 URL 的一部分。

_们稍微修改示例,以便您可以将一些参数信息从 URL 中传递给控制器 (例如, HelloWorld/欢迎? 名称 = 斯科特 & numtimes = 4)。 Welcome method to include two parameters as shown below. " _mstHash="2516688" _mstChunk="true">改变你 Welcome 方法包括两个参数,如下所示。 parameter should default to 1 if no value is passed for that parameter." _mstHash="3775032" _mstChunk="true">注意,该代码使用 C# 的可选参数功能表明numTimes 参数应默认为 1,如果没有传递值,该参数。

public string Welcome(string name, int numTimes = 1) {
     return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);
}
安全说明: 上面的代码使用HttpServerUtility.HtmlEncode来保护应用程序的恶意输入 (即 JavaScript)。详细信息请参阅如何: 在 Web 应用程序,通过应用 HTML 编码的字符串防止脚本侵入.

name and numtimes in the URL. The ASP.NET MVC model binding system automatically maps the named parameters from the query string in the address bar to parameters in your method." _mstHash="1076465" _mstChunk="true">运行您的应用程序,然后浏览到示例 URL (http://localhost:xxxx,HelloWorld,欢迎光临? 名称 = 斯科特 & numtimes = 4) name and numtimes in the URL. " _mstHash="2152930" _mstChunk="true">你可以尝试不同的值 name numtimes 在 URL 中。ASP.NET MVC 模型绑定系统将自动映射到您的方法中的参数从地址栏中的查询字符串的命名的参数。

在上面的示例中,URL 段 (Parameters) 而不使用,作为查询字符串传递的参数name numTimes 参数。吗?(问号) 在上面的 URL 是分隔符,接着的查询字符串。& 字符分隔的查询字符串。

欢迎的方法替换为以下代码:

public string Welcome(string name, int ID = 1)
{
    return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);
}

运行应用程序并输入以下 URL: http://localhost:xxx/HelloWorld/欢迎/3? 名称 = Rick

这次第三部分是 URL 匹配路由参数ID.Welcome 的操作方法包含匹配 URL 规范在RegisterRoutes 方法中的参数 (ID)。

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

and numtimes in parameters as route data in the URL. In the App_StartRouteConfig.cs file, add the "Hello" route:" _mstHash="1292123">在 ASP.NET MVC 应用程序中,它是更常见的做法在作为路由数据 (像我们一样具有 ID 以上) 比将它们作为查询字符串传递的参数中传递。 and numtimes in parameters as route data in the URL. " _mstHash="2584246" _mstChunk="true">您也可以添加一条路线,通过这两个name numtimes 作为路由数据的 URL 中的参数。App_StartRouteConfig.cs文件中,添加"Hello"路由:

public class RouteConfig
{
   public static void RegisterRoutes(RouteCollection routes)
   {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

      routes.MapRoute(
          name: "Default",
          url: "{controller}/{action}/{id}",
          defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
      );

      routes.MapRoute(
           name: "Hello",
           url: "{controller}/{action}/{name}/{id}"
       );
   }
}

运行应用程序并浏览到/localhost:XXX/HelloWorld/Welcome/Scott/3.

对于许多的 MVC 应用程序,默认路由工作正常。您将了解在本教程后面来传递数据使用模型联编程序,和你不需要修改默认路由的。

在这些例子中,控制器做 MVC 的"VC"部分 — — 那就是,视图和控制器的工作。控制器直接返回 HTML。通常你不想控制器直接返回 HTML,因为,变得非常繁琐的代码。而我们通常会使用一个单独的视图模板文件

原文地址:https://www.cnblogs.com/275147378abc/p/4755063.html