简体   繁体   English

向 NET Core Api 添加剃刀视图

[英]Adding razor views to NET Core Api

I am building an app .NET Core Api + Angular app that requires send emails to their users.我正在构建一个需要向用户发送电子邮件的应用程序 .NET Core Api + Angular 应用程序。 The feature is working okay using html templates and an ugly code to replace the conent in these files (there are at least 4 html templates).该功能可以使用 html 模板和丑陋的代码来替换这些文件中的内容(至少有 4 个 html 模板)。 Now I want to do the thing better and I want to use razor views as my "mail templates" and I am following the article here现在我想把这件事做得更好,我想使用剃刀视图作为我的“邮件模板”,我正在关注这里的文章

Basically the code is using 3 projects: razor pages / standard libray /Razor class library but in my case I only want add these "cshtml" files all inside of my api project.基本上代码使用了 3 个项目:razor 页面/标准库/Razor 类库,但在我的情况下,我只想在我的 api 项目中添加这些“cshtml”文件。 So that I did was create a folder Views and inside of it all the required razor views.所以我所做的是创建一个文件夹 Views 并在其中创建所有必需的剃刀视图。 After that I published these razor views to test if the project this kind of project understand that it must move the folder and files to the final destination BUT nothing happen.之后,我发布了这些 razor 视图以测试该项目是否理解它必须将文件夹和文件移动到最终目的地,但没有任何反应。

Also I did some litle changes in my StartUp class in order to inform to VS that this project is also supporting MVC此外,我在 StartUp 类中做了一些小改动,以通知 VS 该项目也支持 MVC

ConfigureServices method:配置服务方法:

   services.AddMvc(config => {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            config.Filters.Add(new AuthorizeFilter(policy));
        });

Configure Method:配置方法:

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

After these change nothing happen.在这些改变之后什么也没有发生。 What is missing?有什么不见了? Update 1: I found this useful link about razor pages and as far I understand Razor files are compiled at both build and publish time and there isn't a way to check that razor files were copied correctly to output folder.更新 1:我发现了这个关于razor 页面的有用链接,据我所知,Razor 文件是在构建和发布时编译的,并且没有办法检查 razor 文件是否已正确复制到输出文件夹。

try this startup class试试这个启动类

public class Startup

{   public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services){
        services.AddMvc(config => {
        var policy = new AuthorizationPolicyBuilder()
            .RequireAuthenticatedUser()
            .Build();
        config.Filters.Add(new AuthorizeFilter(policy));
       });
    }

    public void Configure(IApplicationBuilder app)
    {
        app.UseStaticFiles(); //this middleware also should be added to the pipeline if your template has images,css etc.
        app.UseMvcWithDefaultRoute();  
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM