asp.net-mvc - angularjs 仅带有 cshtml 页面,而不是带有 web api 2 的 html 页面

标签 asp.net-mvc angularjs asp.net-mvc-4 asp.net-web-api asp.net-mvc-routing

我有asp。净 mvc4 项目。 Angularjs 已集成。
我已经按照以前的要求构建了 HTML 页面和 WEB API 2。

现在出于某种原因,我必须使用 CSHTML 页面。以前我只有 web api 项目模板,所以我不能使用 cshtml 页面,因为只有 MVC Controller 可以返回部分页面(cshtml),但我只使用 web apis ...

所以改变了整个项目模板,所以现在我可以拥有 mvc Controller ......

简而言之,目前我有已经构建的 web apis 和 html 页面的 mvc4 项目.....

我只想使用 CSHTML 页面而不是使用 HTML 页面,其余的东西应该是这样的......

是否可以?

我可能听起来很傻,但现在需要它。帮助将不胜感激...

仅限 Web api、cshtml 页面和 angularjs。不是 web api、html 页面和 angularjs。

如果我更换 html页面来自 cshtml, Angular 路线将如何受到影响?

最佳答案

嗯,这就是我如何使用它。

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>

注意:使用 Angular 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 开始 angular或 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-mvc - angularjs 仅带有 cshtml 页面,而不是带有 web api 2 的 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27675308/

相关文章:

javascript - 简单的 AngularJS 1.5.9 组件不在 MVC 5 中呈现

c# - 注销被禁止的用户

python - django-rest-framework 如何使模型序列化程序字段成为必需的

angularjs - 将 Silverlight APP 转换为 HTML5 SPA – 工具和技术

c# - Google Analytics(分析)如何确定用户是否移动?

asp.net-mvc-4 - MVC4 - 如何将 View 呈现为字符串?

c# - Asp.net mvc 密码校验 : Weak, 正常,强

javascript - ASP.NET MVC 将模型传递给 KnockoutJS 外部文件

javascript - Angular.js 和 Node.js 在页面加载时调用 Node

c# - 身份验证和登录页面 - 我如何进行身份验证?