angularjs与CSHTML页面只不是HTML与网页API 2页 [英] angularjs with cshtml page only not html pages with web api 2
问题描述
我有ASP。净mvc4项目。 Angularjs集成。
我已经建立HTML页面和WEB的API 2按previous要求。
I'm having asp. net mvc4 project. Angularjs is integrated. I have already built HTML pages and WEB APIs 2 as per previous requirements.
现在由于某种原因,我必须去与CSHTML页面。 previously我只有网页API项目模板,所以我想不出有CSHTML页面,因为只有MVC控制器去可以返回部分页面(CSHTML),但我只用网络的API ...
Now for some reason i have to go with CSHTML pages. previously I had only web api project template so i couldnt go with cshtml pages as ONLY MVC controller can return partial page(cshtml) but i was using only web apis...
所以,改变整个项目的模板,所以现在我可以有MVC控制器......
So changed whole project template and so now i can have mvc controllers... ...
总之,目前我有mvc4项目,已建成的网络API和HTML页面.....
In short, currently I have mvc4 project with already built web apis and html pages.....
我只是想用CSHTML页面,而不是使用HTML网页其余都是应该的,因为他们是...
I just want to use CSHTML pages instead of using HTML pages rest things should be as they are...
这可能吗?
我可能听起来很傻,但它现在需要的。帮助将不胜AP preciated ...
I may sound silly but it is need right now. help would be greatly appreciated...
只有网络的API,CSHTML页面和angularjs。没有网络的API,HTML页面和angularjs。
Web apis, cshtml pages and angularjs only. not web apis, html pages and angularjs.
如果我取代 HTML
页 CSHTML,
将如何角航线会受到影响?
If I replace html
pages by cshtml,
how would angular route get affected?
推荐答案
好吧,这是我如何使用它。
Well, this is how I use it.
有 index.cshtml
像这样
There is index.cshtml
like this
@using System.Web.Optimization
@inherits System.Web.Mvc.WebViewPage
@{ Layout = null; }<!DOCTYPE html>
<html data-ng-app="MyProject">
<head>
<title data-ng-controller="TitleCtrl" data-ng-bind="Title">My Project</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="cache-control" content="max-age=0" />
<meta http-equiv="cache-control" content="no-cache" />
<meta http-equiv="expires" content="0" />
<meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" />
<meta http-equiv="pragma" content="no-cache" />
@Styles.Render("~/Content/min/css")
</head>
<body class="body" id="body">
<div class="body" data-ui-view="body"></div>
@Scripts.Render("~/js/angular")
@Scripts.Render("~/js/MyProject")
</body>
</html>
的注:使用角 UI-路由器
,这就是为什么用户界面视图
是。安迪
NOTE: using angular UI-Router
, that's why ui-view
is in palce
但仍然有一定的 的HomeController
But still there must be 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");
}
}
所以,这样我可以使用捆绑configureation(JavaScript的,CSS)...的功率,而在 HTTP角度出发:// MYDOMAIN /应用
或的http:// MYDOMAIN /应用/
。的检查也类似这里 的
此外在Global.asax中,我们应该CONFIGURA ASP.NET MVC:
Also in global.asax, we should configura 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应该是:
while web API should be:
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 }
);
这篇关于angularjs与CSHTML页面只不是HTML与网页API 2页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!