zoukankan      html  css  js  c++  java
  • C#高级编程笔记 2016年10月26日 MVC入门 Controller

    1、MVC的定义:


     
    • Models: Classes that represent the data of the application  and that use validation logic to enforce business rules for that data.
    • Views: Template files that your application uses to dynamically  generate HTML responses.
    • Controllers: Classes that handle incoming browser requests,  retrieve model data, and then specify view templates that return a response  to the browser.
    2、MVC的路由调用:(在 App_Start中的RouteConfig.cs 中定义了路由调用的基本格式)

         RouteConfig.cs
    using System.Web.Mvc;
    using System.Web.Routing;
     
    namespace MvcWebApplication
    {
        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 }//当url中无输入时,默认到Home 控制器中的Index  Action 里面
                );
            }
        }
    }
         
         ASP.NET MVC invokes different controller classes (and different action methods within  them) depending on the incoming URL. The default URL routing logic used by ASP.NET  MVC uses a format like this to determine what code to invoke:
     
    / [Controller] / [ActionName] / [Parameters]
     
         When you run the application and don't supply any URL segments, it defaults to  the "Home" controller and the "Index" action method specified in the defaults  section of the code above. 
      [Controller]
         The first part of the URL determines the controller class to execute. So /HelloWorld maps  to the HelloWorldController class. 
     
         [ActionName]
      The second part of the URL determines the action method on the class to execute.  So /HelloWorld/Index would  cause the Index method of the HelloWorldController class to execute. Notice that we only had to browse to /HelloWorld and  the Index method  was used by default. This is because a method named Index is the default method that will be called on a controller if one is not explicitly specified.
         [Parameters]
      The third part of the URL segment ( Parameters) is for route data. We'll see route data later on in this  tutorial.
     
         Browse to http://localhost:xxxx/HelloWorld/Welcome.  The Welcome method  runs and returns the string "This is the Welcome action method...". The  default MVC mapping is /[Controller]/[ActionName]/[Parameters].  For this URL, the controller is HelloWorld and Welcome is  the action method. You haven't used the [Parameters] part  of the URL yet.
     
    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 }
        );}
    3、HttpUtility:System.Web.HttpUtility
     

         提供用于在处理Web请求时编码和解码 URL 的方法,此类不能被继承。
     
         
    4、/ [Controller] [ActionName] / [Parameters] 的理解

       
          Let's modify the example slightly so that you can pass some parameter information  from the URL to the controller (for example, /HelloWorld/Welcome?name=Scott&numtimes=4).  Change your Welcome method  to include two parameters as shown below. Note that the code 
     
    public string Welcome(string name, int numTimes = 1) {
        return HttpUtility.HtmlEncode("Hello " + name + ", NumTimes is: " + numTimes);}
         
         Run your application and browse to the example URL (http://localhost:xxxx/HelloWorld/Welcome?name=Scott&numtimes=4).  You can try different values for 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.
     
     
    Replace the Welcome method with the following code:
     
    public string Welcome(string name, int ID = 1){
        return HttpUtility.HtmlEncode("Hello " + name + ", ID: " + ID);}
     
    Run the application and enter the following URL:  http://localhost:xxx/HelloWorld/Welcome/3?name=Rick
     

     
    ##为什么这时的“ 3 ” 能够被识别,而不用输入 "gender = 3" 呢?
         这是路由中的源码:
         
    1 public static void RegisterRoutes(RouteCollection routes){
    2     routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    3 
    4     routes.MapRoute(
    5         name: "Default",
    6         url: "{controller}/{action}/{id}",
    7         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    8     );}
     
         This time the third URL segment  matched the route parameter ID. The Welcome action method contains a parameter  (ID) that matched the URL specification in the RegisterRoutes method.
         在 url 中有参数 id , 于是MVC 便会自动匹配。
     
          In ASP.NET MVC applications, it's more typical to pass in parameters as route  data (like we did with ID above) than passing them as query strings. You could also  add a route to pass both the name and numtimes in  parameters as route data in the URL. In the App_StartRouteConfig.cs  file, add the "Hello" route:
     
    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}"
           );
       }}
     
          Run the application and browse to /localhost:XXX/HelloWorld/Welcome/Scott/3.
     
     
     
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    navigateTo防止多次跳转
    vue中的绑定class和微信小程序中的绑定class的区别
    js同步和异步
    本地存储和vuex使用对比
    微信小程序页面跳转区别总结
    CAS-技术专区-认证服务器cas-server搭建
    CAS-技术专区-SSO配置完整案例(静态认证+数据库认证)
    SpringCloud-技术专区-实战案例-Zuul整合OAuth2.0认证服务
    OAuth2.0协议专区-SpringCloud安全-集成OAuth2实现身份认证和单点登录
    OAuth2.0协议专区-SpringCloud微服务实战-基于OAUTH2.0统一认证授权的微服务基础架构
  • 原文地址:https://www.cnblogs.com/xiyin/p/6000184.html
Copyright © 2011-2022 走看看