asp.net-mvc – 使用cshtml页面的angularjs不是带有web api 2的html页面
|
我有asp. net mvc4项目. Angularjs是整合的.
现在由于某种原因,我必须使用CSHTML页面.以前我只有web api项目模板所以我不能用cshtml页面,因为只有MVC控制器可以返回部分页面(cshtml),但我只使用web apis … 所以改变了整个项目模板,所以现在我可以拥有mvc控制器…… 总之,目前我已经建立了web apis和html页面的mvc4项目….. 我只是想使用CSHTML页面而不是使用HTML页面休息的东西应该是它们…… 可能吗? 我可能听起来很傻但现在需要它.帮助将不胜感激…… 仅限Web apis,cshtml页面和angularjs.不是web apis,html pages和angularjs. 如果我用cshtml替换html页面,角度路由会受到什么影响? 解决方法嗯,这就是我使用它的方式.有像这样的index.cshtml @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-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 }
); (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – Global.asax的方法来自哪里?
- asp.net – 以编程方式将Textbox TemplateField列添加到Gri
- asp.net-core – 在ASP.NET Core中将html导出为pdf
- asp.net-mvc – 如何在单个视图中使用两个表单
- .net – IDictionary到SelectList?
- asp.net – 如何以编程方式将参数传递给SSRS报告
- asp.net-mvc – 使用MiniProfiler与MVC 5
- asp.net – 如何.NET网站隐藏它们的文件的.aspx扩展名?
- asp.net-mvc-3 – 与ASP.NET MVC 3中的视图页面不同的模型的
- asp.net删除文件session丢失
- 使用ASP.Net Web API进行多部分表单POST
- 深蓝词库转换1.6发布
- asp.net-mvc – 如何清除ASP.NET MVC应用程序中文
- 当使用SignalR和传输模式长轮询时,Asp.net会话永
- asp.net – HttpError iis config在添加默认路径
- asp.net-mvc – VIEWDATA和VIEWBAG存储在MVC中的
- asp.net-mvc – 如何使用Html.DropDownList为默认
- .net – 只有在配置中enableSessionState设置为t
- asp.net – 元素 system.webServer’有无效的chi
- asp.net – 诸如Eval(),XPath()和Bind()的数据绑
