栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

VS2017中建立ASP.NET MVC 4.0项目

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

VS2017中建立ASP.NET MVC 4.0项目

新的项目需要运行在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;
}








这是MVC示例页
@ViewBag.mes


5.在Views文件夹下建立Web.config文件,内容:






















" verb="" type="System.Web.HttpNotFoundHandler"/>


  
    
  



  
  



感觉和默认的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中的智能提示是没有的,不知道怎么弄,先这样吧!!!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/231072.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号