I. Use View data
Add the following code to the Controller:
public class ProductController : Controller { // // GET: /Product/ public ActionResult Index() { ViewData["Message"] = "Hello World"; return View(); } }
Then add the code to the View:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<ToyStore.Models.Products>" %><asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server"> Index</asp:Content><asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
The content is passed, but because the input value may be of any type, you have to convert the type yourself. Haha.
Viewdata. model allows you to have a strong type.
Viewdata. Model = _ datamodel. productset. tolist ();
Ii. prevent javascript injection attacks
By default, it is preventive. If you do not want to use it, check the following code:
[ValidateInput(false)] [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude="Id")]Product productToCreate) { if (!ModelState.IsValid) return View(); try { _dataModel.AddToProductSet(productToCreate); _dataModel.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); } }
Mainly [validateinput (false)]
Iii. Replaceable view Engine
The view engine in Asp.net MVC can be replaced. The default engine is called the web forms view engine. Now there are several open-source view engines.
Http://www.codeplex.com/MVCContrib can look for it here, a website can use a few view engine at the same time, can be in the global. in asax, different extensions are used to differentiate different view engines, for example :. the ASPX page uses the web forms view engine.
We can also customize a view engine and inherit a class from virtualpathproviderviewengine.
using System.Web.Mvc; namespace MvcApplication1.MyViewEngines { public class SimpleViewEngine : VirtualPathProviderViewEngine { public SimpleViewEngine() { this.ViewLocationFormats = new string[] { "~/Views/{1}/{0}.simple", "~/Views/Shared/{0}.simple"}; this.PartialViewLocationFormats = new string[] { "~/Views/{1}/{0}.simple", "~/Views/Shared/{0}.simple" }; } protected override IView CreateView(ControllerContext controllerContext, string viewPath, string masterPath) { var physicalPath = controllerContext.HttpContext.Server.MapPath(viewPath); return new SimpleView(physicalPath); } protected override IView CreatePartialView(ControllerContext controllerContext, string partialPath) { var physicalPath = controllerContext.HttpContext.Server.MapPath(partialPath); return new SimpleView(physicalPath); } } }
Write in global. asax
protected void Application_Start() { RegisterRoutes(RouteTable.Routes); ViewEngines.Engines.Add(new SimpleViewEngine()); }
Then write a view page class:
namespace MvcApplication1.MyViewEngines { public class SimpleView : IView { private string _viewPhysicalPath; public SimpleView(string viewPhysicalPath) { _viewPhysicalPath = viewPhysicalPath; } #region IView Members public void Render(ViewContext viewContext, TextWriter writer) { // Load file string rawContents = File.ReadAllText(_viewPhysicalPath); // Perform replacements string parsedContents = Parse(rawContents, viewContext.ViewData); // Write results to HttpContext writer.Write(parsedContents); } #endregion public string Parse(string contents, ViewDataDictionary viewData) { return Regex.Replace(contents, "//{(.+)//}",m => GetMatch(m, viewData)); } protected virtual string GetMatch(Match m, ViewDataDictionary viewData) { if (m.Success) { string key = m.Result("$1"); if (viewData.ContainsKey(key)) { return viewData[key].ToString(); } } return string.Empty; } } }
Define a controller:
using System.Web.Mvc; namespace MvcApplication1.Controllers { public class SimpleController : Controller { public ActionResult Index() { ViewData["message"] = "Hello World!"; return View(); } } }
The page can be written as views/simple/index. Simple
{Message} is parsed.