zoukankan      html  css  js  c++  java
  • .Net视图机制

    .Net会有默认的约定。

    HomeController下面的Index,会默认渲染Home/Index.cshtml。

    当然可以设置成别的,比如设置成About。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace mvcDemo.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "I like milk.";
                return View("About");
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "I like cake.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
        }
    }
    

    这样首页就使用了About的视图页面了。

    视图被控制器渲染,控制器提供视图数据,这是.Net约定俗成的。

    改为完整路径,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace mvcDemo.Controllers
    {
        public class HomeController : Controller
        {
            public ActionResult Index()
            {
                ViewBag.Message = "I like milk.";
                return View("~/Views/Home/About.cshtml");
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "I like cake.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
        }
    }
    
  • 相关阅读:
    CCF CSP 201609-2 火车购票
    CCF CSP 201409-2 画图
    CCF CSP 201409-2 画图
    CCF CSP 201409-4 最优配餐
    CCF CSP 201409-4 最优配餐
    CCF CSP 201503-1 图像旋转
    CCF CSP 201503-1 图像旋转
    Ethical Hacking
    Ethical Hacking
    Ethical Hacking
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6874865.html
Copyright © 2011-2022 走看看