【问题标题】:Is there a way to convert my Angular ASP.net Core template to ASP.net core MVC有没有办法将我的 Angular ASP.net Core 模板转换为 ASP.net core MVC
【发布时间】:2020-08-23 13:57:12
【问题描述】:

我想为我的 Angular 项目构建一个 web api,我选择了 ASP.net。我已经使用了 Angular Asp.net 核心模板,并且我几乎完成了 Angular 前端。我想知道是否需要将模板转换为 MVC?如果是,可以转换它还是我需要从头开始?如果我从头开始后端,有没有办法拔掉 Angular 前端?

【问题讨论】:

  • 你可以只使用 .Net Core2.2 而不是 3.0

标签: c# asp.net angular asp.net-mvc asp.net-core


【解决方案1】:

如果你创建了一个新的 MVC 项目(确保为你的后端包含 WebApi 等),你可以复制:

src //Directory
angular.json
browserslist
package-lock.json
package.json
tsconfig.*

它们应该放在 MVC 项目的根级别,并且应该看起来像图像

编辑_layout.chtml

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="Cache-control" content="no-cache, no-store, must-revalidate">
    <meta http-equiv="Pragma" content="no-cache">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500&display=swap" rel="stylesheet">
    <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
    <title>@ViewBag.Title</title>
</head>
<body class="mat-typography">
@RenderBody()

@Scripts.Render("~/bundles/angular")
</body>
</html>

并创建 bundleconfig.cs 来提供您的文件:

public class BundleConfig
{
    public static void RegisterBundles(BundleCollection bundles)
    {
        bundles.Add(new Bundle("~/bundles/angular").Include(
            "~/Scripts/lib/runtime*",
            "~/Scripts/lib/polyfills*",
            "~/Scripts/lib/styles*",
            "~/Scripts/lib/scripts*",
            "~/Scripts/lib/vendor*",
            "~/Scripts/lib/main*"));
    }
}

终于更新Index.cshtml

@{
    ViewBag.Title = "My Angular App";
}
<app-root></app-root>

如果你还没有在你的 Angular 应用程序中启用哈希 URL(app-routing.module.ts)

@NgModule({
    imports: [RouterModule.forRoot(routes, { useHash: true })],
    exports: [RouterModule]
})
export class AppRoutingModule { }

【讨论】:

  • 我也可以添加节点模块吗?
  • 错误 CS0103 名称“脚本”在当前上下文中不存在 C:\Users\...\source\repos\...\...\Views\Shared_Layout.cshtm 我有完成了以下步骤,我收到了 _layout.cshtml 的这个错误你知道这个问题的解决方案吗?非常感谢
  • 当我回到笔记本电脑时,我会更新我的答案,但您需要更新默认输出到的位置,它会化为灰烬,需要更改为 Scripts/lib。应该在你的 angular.json 文件中找到
  • 你可以移动 node_modules 或者直接退出 npm install ,它们会被再次拉取
猜你喜欢
  • 2023-02-02
  • 2021-05-13
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
相关资源
最近更新 更多