從ASP.NET WebFrom轉移到MVC
跳至導覽
跳至搜尋
從ASP.NET WebFrom轉移到MVC, 以下使用VS 2013 Pro用C#開WebForm專案加上MVC與WebAPI支援之後。
相關檔案
Web.config
Web.config
<?xml version="1.0" encoding="utf-8"?> <!-- 如需如何設定 ASP.NET 應用程式的詳細資訊,請造訪 http://go.microsoft.com/fwlink/?LinkId=169433 --> <configuration> <configSections> <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --> <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" /> </configSections> <appSettings> <add key="webpages:Version" value="3.0.0.0" /> <add key="webpages:Enabled" value="false" /> <add key="PreserveLoginUrl" value="true" /> <add key="ClientValidationEnabled" value="true" /> <add key="UnobtrusiveJavaScriptEnabled" value="true" /> </appSettings> <connectionStrings> <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-WebApp1-20131223033236.mdf;Initial Catalog=aspnet-WebApp1-20131223033236;Integrated Security=True" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web> <authentication mode="None" /> <compilation debug="true" targetFramework="4.5.1" /> <httpRuntime targetFramework="4.5.1" /> <pages> <namespaces> <add namespace="System.Web.Helpers" /> <add namespace="System.Web.Mvc" /> <add namespace="System.Web.Mvc.Ajax" /> <add namespace="System.Web.Mvc.Html" /> <add namespace="System.Web.Optimization" /> <add namespace="System.Web.Routing" /> <add namespace="System.Web.WebPages" /> <add namespace="Microsoft.AspNet.Identity" /> </namespaces> <controls> <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" /> </controls> </pages> <membership> <providers> <!-- 已在此範本中停用 ASP.NET 成員。請造訪以下連結 http://go.microsoft.com/fwlink/?LinkId=301889 以深入了解此範本支援的 ASP.NET 成員 --> <clear /> </providers> </membership> <profile> <providers> <!-- 已在此範本中停用 ASP.NET 成員設定檔。請造訪以下連結 http://go.microsoft.com/fwlink/?LinkId=301889 以深入了解此範本支援的 ASP.NET 成員 --> <clear /> </providers> </profile> <roleManager> <!-- 已在此範本中停用 ASP.NET 成員角色。請造訪以下連結 http://go.microsoft.com/fwlink/?LinkId=301889 以深入了解此範本支援的 ASP.NET 成員 --> <providers> <clear /> </providers> </roleManager> <!-- 已在此範本中停用 ASP.NET 成員角色。請造訪以下連結 http://go.microsoft.com/fwlink/?LinkId=301889 以深入了解此範本支援的 ASP.NET 成員 --> <sessionState mode="InProc" customProvider="DefaultSessionProvider"> <providers> <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" /> </providers> </sessionState> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false" /> <modules> <remove name="FormsAuthenticationModule" /> </modules> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> </system.webServer> <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" /> <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" /> </dependentAssembly> </assemblyBinding> </runtime> <entityFramework> <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework"> <parameters> <parameter value="v11.0" /> </parameters> </defaultConnectionFactory> <providers> <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" /> </providers> </entityFramework> </configuration>
Global.asax
Global.asax
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Optimization; using System.Web.Routing; using System.Web.Security; using System.Web.SessionState; using System.Web.Http; namespace WebApp1 { public class Global : HttpApplication { void Application_Start(object sender, EventArgs e) { // 應用程式啟動時執行的程式碼 AreaRegistration.RegisterAllAreas(); GlobalConfiguration.Configure(WebApiConfig.Register); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); } } }
packages.config
packages.config
<?xml version="1.0" encoding="utf-8"?> <packages> <package id="Antlr" version="3.4.1.9004" targetFramework="net451" /> <package id="AspNet.ScriptManager.bootstrap" version="3.0.0" targetFramework="net451" /> <package id="AspNet.ScriptManager.jQuery" version="1.10.2" targetFramework="net451" /> <package id="bootstrap" version="3.0.0" targetFramework="net451" /> <package id="EntityFramework" version="6.0.0" targetFramework="net451" /> <package id="EntityFramework.zh-Hant" version="6.0.0" targetFramework="net451" /> <package id="jQuery" version="1.10.2" targetFramework="net451" /> <package id="Microsoft.AspNet.FriendlyUrls" version="1.0.2" targetFramework="net451" /> <package id="Microsoft.AspNet.FriendlyUrls.Core" version="1.0.2" targetFramework="net451" /> <package id="Microsoft.AspNet.FriendlyUrls.Core.zh-Hant" version="1.0.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.Core" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.Core.zh-Hant" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.EntityFramework" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.EntityFramework.zh-Hant" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.Owin" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Identity.Owin.zh-Hant" version="1.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Mvc" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Mvc.zh-Hant" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Providers.Core" version="1.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Providers.Core.zh-Hant" version="1.2" targetFramework="net451" /> <package id="Microsoft.AspNet.Razor" version="3.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Razor.zh-Hant" version="3.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.ScriptManager.MSAjax" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.ScriptManager.WebForms" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.Web.Optimization" version="1.1.1" targetFramework="net451" /> <package id="Microsoft.AspNet.Web.Optimization.WebForms" version="1.1.1" targetFramework="net451" /> <package id="Microsoft.AspNet.Web.Optimization.zh-Hant" version="1.1.1" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Client" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Client.zh-Hant" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Core" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.Core.zh-Hant" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.WebHost" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebApi.WebHost.zh-Hant" version="5.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebPages" version="3.0.0" targetFramework="net451" /> <package id="Microsoft.AspNet.WebPages.zh-Hant" version="3.0.0" targetFramework="net451" /> <package id="Microsoft.Owin" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Host.SystemWeb" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Host.SystemWeb.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Cookies" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Facebook" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Facebook.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Google" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Google.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.MicrosoftAccount" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.MicrosoftAccount.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.OAuth" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Twitter" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.Twitter.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.Security.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Owin.zh-Hant" version="2.0.0" targetFramework="net451" /> <package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net451" /> <package id="Modernizr" version="2.6.2" targetFramework="net451" /> <package id="Newtonsoft.Json" version="5.0.6" targetFramework="net451" /> <package id="Owin" version="1.0" targetFramework="net451" /> <package id="Respond" version="1.2.0" targetFramework="net451" /> <package id="WebGrease" version="1.5.2" targetFramework="net451" /> </packages>
/App_Start/BundleConfig.cs
/App_Start/BundleConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Optimization; using System.Web.UI; namespace WebApp1 { public class BundleConfig { // 如需統合的詳細資訊,請造訪 http://go.microsoft.com/fwlink/?LinkID=303951 public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new ScriptBundle("~/bundles/WebFormsJs").Include( "~/Scripts/WebForms/WebForms.js", "~/Scripts/WebForms/WebUIValidation.js", "~/Scripts/WebForms/MenuStandards.js", "~/Scripts/WebForms/Focus.js", "~/Scripts/WebForms/GridView.js", "~/Scripts/WebForms/DetailsView.js", "~/Scripts/WebForms/TreeView.js", "~/Scripts/WebForms/WebParts.js")); // 順序對於這些檔案產生作用而言相當重要,它們有明確的相依性 bundles.Add(new ScriptBundle("~/bundles/MsAjaxJs").Include( "~/Scripts/WebForms/MsAjax/MicrosoftAjax.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxApplicationServices.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxTimer.js", "~/Scripts/WebForms/MsAjax/MicrosoftAjaxWebForms.js")); // 使用 Modernizr 的開發版本來開發並深入了解。當您準備好量產時, // 請使用 http://modernizr.com 中的建置工具來挑選出您需要的測試 bundles.Add(new ScriptBundle("~/bundles/modernizr").Include( "~/Scripts/modernizr-*")); ScriptManager.ScriptResourceMapping.AddDefinition( "respond", new ScriptResourceDefinition { Path = "~/Scripts/respond.min.js", DebugPath = "~/Scripts/respond.js", }); } } }
/App_Start/RouteConfig.cs
/App_Start/RouteConfig.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.Mvc; using System.Web.Routing; using Microsoft.AspNet.FriendlyUrls; namespace WebApp1 { public static class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { var settings = new FriendlyUrlSettings(); settings.AutoRedirectMode = RedirectMode.Permanent; routes.EnableFriendlyUrls(settings); routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { action = "Index", id = UrlParameter.Optional } ); } } }
/App_Start/Startup.Auth.cs
/App_Start/Startup.Auth.cs
using Microsoft.AspNet.Identity; using Microsoft.Owin; using Microsoft.Owin.Security.Cookies; using Owin; namespace WebApp1 { public partial class Startup { // 如需設定驗證的詳細資訊,請造訪 http://go.microsoft.com/fwlink/?LinkId=301883 public void ConfigureAuth(IAppBuilder app) { // 啟用應用程式以使用 Cookie 來儲存已登入使用者的資訊 // 也儲存透過第三方登入提供者登入的使用者資訊。 // 若您的應用程式允許使用者登入,則此為必要項目 app.UseCookieAuthentication(new CookieAuthenticationOptions { AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, LoginPath = new PathString("/Account/Login") }); app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); // 取消註解下列行以啟用透過第三方登入提供者的登入 //app.UseMicrosoftAccountAuthentication( // clientId: "", // clientSecret: ""); //app.UseTwitterAuthentication( // consumerKey: "", // consumerSecret: ""); //app.UseFacebookAuthentication( // appId: "", // appSecret: ""); //app.UseGoogleAuthentication(); } } }
/App_Start/WebApiConfig.cs
/App_Start/WebApiConfig.cs
using System; using System.Collections.Generic; using System.Linq; using System.Web.Http; namespace WebApp1 { public static class WebApiConfig { public static void Register(HttpConfiguration config) { // Web API configuration and services // Web API routes config.MapHttpAttributeRoutes(); config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{id}", defaults: new { id = RouteParameter.Optional } ); } } }