zoukankan      html  css  js  c++  java
  • Spring.Web.Mvc 注入(控制器属性注入)

    1.web.config配置

    <?xml version="1.0" encoding="utf-8"?>
    <!--
    有关如何配置 ASP.NET 应用程序的详细信息,请访问
    http://go.microsoft.com/fwlink/?LinkId=301880
    -->
    <configuration>
    <configSections>
    <sectionGroup name="spring">
    <section name="context" type="Spring.Context.Support.MvcContextHandler, Spring.Web.Mvc"/>
    </sectionGroup>
    </configSections>

    <connectionStrings>
    <add name="ApplicationServices"
    connectionString="data source=.SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
    providerName="System.Data.SqlClient" />
    </connectionStrings>

    <spring>
    <context>
    <resource uri="~/Config/Controllers.xml"/>
    </context>
    </spring>
    <appSettings>
    <add key="webpages:Version" value="3.0.0.0"/>
    <add key="webpages:Enabled" value="false"/>
    <add key="ClientValidationEnabled" value="true"/>
    <add key="UnobtrusiveJavaScriptEnabled" value="true"/>
    </appSettings>
    <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    </system.web>
    <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
    <dependentAssembly>
    <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
    <bindingRedirect oldVersion="1.0.0.0-5.2.3.0" newVersion="5.2.3.0"/>
    </dependentAssembly>
    </assemblyBinding>
    </runtime>
    <system.codedom>
    <compilers>
    <compiler language="c#;cs;csharp" extension=".cs"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
    <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
    type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=&quot;Web&quot; /optionInfer+"/>
    </compilers>
    </system.codedom>
    </configuration>

    2.根目录添加lib文件夹(spring.core spring.web.mvc Common.Logging.dll Common.Logging.Log4Net.dll)

    程序集引用添加这4个东西

    3.mvcapplication 注册路由重写 继承SpringMvcApplication

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Spring.Web.Mvc;

    namespace WebApplication12
    {
    public class MvcApplication :SpringMvcApplication
    {
    protected override 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 }
    );
    }
    //protected void Application_Start()
    //{
    // AreaRegistration.RegisterAllAreas();
    // RouteConfig.RegisterRoutes(RouteTable.Routes);
    //}
    }
    }

    3.根目录下新建Config文件夹 里面添加Controllers.xml(右键属性 始终复制 我的迷信)

    <?xml version="1.0" encoding="utf-8" ?>
    <objects xmlns="http://www.springframework.net">

    <object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
    <property name="Message1" value="海贼王的乱入" />
    </object>
      <!--集合注入-->

    <object type="WebApplication12.Controllers.HomeController, WebApplication12" singleton="false" >
    <property name="Message" >
    <list element-type="System.String">
    <value>B0 Stage</value>
    <value>B1 Stage</value>
    <value>B2 Stage</value>
    <value>B3 Stage</value>
    <value>B4 Stage</value>
    <value>B5 Stage</value>
    <value>B6 Stage</value>
    </list>
    </property>
    </object>

    </objects>

    4.控制器添加Message属性之后用于注入

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace WebApplication12.Controllers
    {
    public class HomeController : Controller
    {
    public string Message { get; set; }
    // GET: Home
    public ActionResult Index()
    {
    ViewData["Message"] = Message;
    return View();
    }
    }
    }

    4.视图调用


    @{
    Layout = null;
    }

    <!DOCTYPE html>

    <html>
    <head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    </head>
    <body>
    <div>
    <h1>@ViewData["Message"]</h1>
    </div>
    </body>
    </html>

  • 相关阅读:
    Atitit 集团与个人的完整入口列表 attilax的完整入口 1. 集团与个人的完整入口列表 1 2. 流量入口概念 2 3. 流量入口的历史与发展 2 1.集团与个人的完整入口列表
    atitit 每季度日程表 每季度流程 v3 qaf.docx Ver history V2 add diary cyar data 3 cate V3 fix detail 3cate ,
    Atitit react 详细使用总结 绑定列表显示 attilax总结 1. 前言 1 1.1. 资料数量在百度内的数量对比 1 1.2. 版本16 v15.6.1 1 1.3. 引入js 2
    Atitit r2017 r3 doc list on home ntpc.docx
    Atitit r2017 ra doc list on home ntpc.docx
    Atiitt attilax掌握的前后技术放在简历里面.docx
    Atitit q2016 qa doc list on home ntpc.docx
    Atitit r7 doc list on home ntpc.docx 驱动器 D 中的卷是 p2soft 卷的序列号是 9AD0D3C8 D:\ati\r2017 v3 r01\
    Atitit 可移植性之道attilax著
    Atitit q2016 q5 doc list on home ntpc.docx
  • 原文地址:https://www.cnblogs.com/kexb/p/4823236.html
Copyright © 2011-2022 走看看