1. 程式人生 > >VS2017中建立ASP.NET MVC 4.0項目

VS2017中建立ASP.NET MVC 4.0項目

mvc4 .html his pathinfo gpo sem device sta 模板

新的項目需要運行在WIN2003上,又不想用ASPX了,只好用回ASP.NET MVC4.0了,可是在VS2017中已經沒有MVC4的模板了,網上下載的安裝了也沒有,只好把以前的MVC4的項目拿 出來看了一下,看看怎麽由空白項目建立起來,步驟如下:

1.VS2017中建立空白的WEB項目,記得選擇.NET 4.0版本的

技術分享圖片

2.NUGET包中搜索ASP.NET MVC,不要下5.0的那個版本,要下4.0的那個版本

技術分享圖片
3.自己手動建立Controllers文件夾,裏面建立HomeController.cs類文件,文件內容
using System;

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

namespace WebApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index() {
ViewBag.mes = "niunan hahaha ...";
return View(); }
}
}

4.自己手動建立Views文件夾,裏面建立Home文件夾,裏面建立Index.cshtml文件,文件內容:
@{
Layout = null;
}

<!DOCTYPE html>

<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>
這是MVC示例頁
@ViewBag.mes
</div>
</body>
</html>

5.在Views文件夾下建立Web.config文件,內容:
<?xml version="1.0"?>

<configuration>
<configSections>
<sectionGroup name="system.web.webPages.razor" type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<section name="host" type="System.Web.WebPages.Razor.Configuration.HostSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
<section name="pages" type="System.Web.WebPages.Razor.Configuration.RazorPagesSection, System.Web.WebPages.Razor, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
</sectionGroup>
</configSections>

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
<namespaces>
<add namespace="System.Web.Mvc" />
<add namespace="System.Web.Mvc.Ajax" />
<add namespace="System.Web.Mvc.Html" />
<add namespace="System.Web.Routing" />
</namespaces>
</pages>
</system.web.webPages.razor>

<appSettings>
<!--<add key="webpages:Enabled" value="false" />-->
<add key="webpages:Version" value="2.0.0.0" />
</appSettings>

<system.web>
<httpHandlers>
<add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
</httpHandlers>

<!--
Enabling request validation in view pages would cause validation to occur
after the input has already been processed by the controller. By default
MVC performs request validation before a controller processes the input.
To change this behavior apply the ValidateInputAttribute to a
controller or action.
-->
<pages
validateRequest="false"
pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
<controls>
<add assembly="System.Web.Mvc, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
</controls>
</pages>
</system.web>

<system.webServer>
<validation validateIntegratedModeConfiguration="false" />

<handlers>
<remove name="BlockViewHandler"/>
<add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
</handlers>
</system.webServer>
</configuration>
感覺和默認的MVC5建立的出來的一樣的,只是把版本號從5.0.0.0改為4.0.0.0了。。。

6.建立App_Start目錄,裏面建立FilterConfig.cs文件,內容:
using System.Web;
using System.Web.Mvc;

namespace WebApplication1
{
public class FilterConfig
{
public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
filters.Add(new HandleErrorAttribute());
}
}
}
感覺好像沒有這個文件也不要緊


7.App_Start目錄中建立RouteConfig.cs文件,內容:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace WebApplication1
{
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 }
);
}
}
}
要用到MVC的,這個文件是最主要的話,用到MVC的路由創建

8.項目根目錄下建立Global.asax文件,在Application_Start中註冊一下上面建立的二個類,主要內容:
protected void Application_Start(object sender, EventArgs e)
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}

9.運行項目,就可以看到界面出來了,不過好像在CSHTML中的智能提示是沒有的,不知道怎麽弄,先這樣吧!!!

VS2017中建立ASP.NET MVC 4.0項目