asp.net mvc web api html,asp.net-mvc – 使用cshtml页面的angularjs不是带有web api 2的html页面...

嗯,这就是我使用它的方式.

有像这样的index.cshtml

@using System.Web.Optimization

@inherits System.Web.Mvc.WebViewPage

@{ Layout = null; }

My Project

@Styles.Render("~/Content/min/css")

@Scripts.Render("~/js/angular")

@Scripts.Render("~/js/MyProject")

注意:使用角度UI-Router,这就是ui-view所在的原因

但仍然必须有HomeController:

public class HomeController : Controller

{

public ActionResult Index()

{

// below I do some tricks to make app running on

// http://mydomain/app as well as on

// http://mydomain/app/ (see the / at the end)

var root = VirtualPathUtility.ToAbsolute("~/");

var applicationPath = Request.ApplicationPath;

var path = Request.Path;

var hasTraillingSlash = root.Equals(applicationPath,StringComparison.InvariantCultureIgnoreCase)

|| !applicationPath.Equals(path,StringComparison.InvariantCultureIgnoreCase);

if (!hasTraillingSlash)

{

return Redirect(root + "#");

}

// my view is not in Views,but in the root of a web project

return View("~/Index.cshtml");

}

}

所以,这样我可以使用Bundle配置(javascript,css)的功能……同时在http:// mydomain / app或http:// mydomain / app /启动角度.检查类似here

同样在global.asax中,我们应该配置ASP.NET MVC:

public static void RegisterRoutes(RouteCollection routes)

{

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.IgnoreRoute("fonts*.woff");

routes.MapRoute(

name: "Default",url: "{controller}/{action}/{id}",defaults: new {controller = "Home",action = "Index",id =UrlParameter.Optional}

);

}

而web API应该是:

const string IdPattern = @"\d+|[A-Za-z]{1,2}";

public static void SetRouting(HttpConfiguration config)

{

// some custom routes

...

// and a default one

config.Routes.MapHttpRoute(

name: "DefaultApi",routeTemplate: "api/{controller}/{id}",constraints: new { id = IdPattern },defaults: new { id = RouteParameter.Optional }

);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值