• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#高级编程笔记2016年10月26日MVC入门Controller

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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_Start\RouteConfig.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.
 
 

 

 
 
 
 
 
 
 
 
 
 

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap