C# 如何为html标题属性设计InfoHelper

C# 如何为html标题属性设计InfoHelper,c#,html,asp.net-mvc,asp.net-core,razor,C#,Html,Asp.net Mvc,Asp.net Core,Razor,对于Asp.net mvc核心应用程序: 在我的一个dbcontext表中,我有HelpInfo记录(id为主键,字符串为HelpInfo),我想在razor视图中将其用作html标题属性,例如: 其中12是HelpInfo记录的示例id,@I应该是一个全局可访问的类方法,用于获取相应的HelpInfo字符串 我尝试实施King Kings方法,例如: 自定义视图: public abstract class CustomView<TModel> : RazorPage<T

对于Asp.net mvc核心应用程序: 在我的一个dbcontext表中,我有HelpInfo记录(id为主键,字符串为HelpInfo),我想在razor视图中将其用作html标题属性,例如:

其中12是HelpInfo记录的示例id,@I应该是一个全局可访问的类方法,用于获取相应的HelpInfo字符串

我尝试实施King Kings方法,例如:

自定义视图

public abstract class CustomView<TModel>  : RazorPage<TModel>
{
    protected IInfo Info => Context.RequestServices.GetRequiredService<IInfo>();

    public Task<string> I(int code)
    {
        return Info.GetAsync(code);
    }
}
public interface IInfo
{
    Task<string> GetAsync(int code);
}
public class Info : IInfo
{
    private readonly ApplicationDbContext context;
    public Info(ApplicationDbContext context)
    {
        this.context = context;
    }

    public async Task<string> GetAsync(int code)
    {
        var myRecord = await context.MyRecords.FindAsync(code);
        if (myRecord != null)
        {
            return myRecord.Info;
        }
        return null;
    }
}

如果您能帮我解决这个问题,我们将不胜感激。

据我所知,您似乎想要一些类似
@Html
@Url
的东西,这些东西在默认的
页面和
RazorPage
中都受支持。这只是一个由基页公开的自定义属性。因此,在您的情况下,您需要一个自定义视图(对于mvc)或一个自定义页面(对于razor页面)。以下是用于MVC的自定义视图示例:

public abstract class CustomView<TModel> : RazorPage<TModel>
{              
    //custom members can be declared in here
}
现在,您的自定义页面可以实现如下
I
方法:

public interface IInfo {
     string Get(int code);
}
public abstract class CustomView<TModel> : RazorPage<TModel>
{      
    //NOTE: the custom page class does not support constructor injection
    //So we need to get the injected services via the Context like this.
    protected IInfo Info => Context.RequestServices.GetRequiredService<IInfo>();        
    public string I(int code){
       return Info.Get(code);
    }
}
@inherits CustomView<TModel>
<div title="@I(12)"></div>
<div title="@(await I(12))"></div>
有时,在重建项目之前,不会应用基础视图(因此基础成员不可用)

现在,您可以根据需要在视图中使用
I
,如下所示:

public interface IInfo {
     string Get(int code);
}
public abstract class CustomView<TModel> : RazorPage<TModel>
{      
    //NOTE: the custom page class does not support constructor injection
    //So we need to get the injected services via the Context like this.
    protected IInfo Info => Context.RequestServices.GetRequiredService<IInfo>();        
    public string I(int code){
       return Info.Get(code);
    }
}
@inherits CustomView<TModel>
<div title="@I(12)"></div>
<div title="@(await I(12))"></div>
I
方法也应该是
async

public Task<string> I(int code){
       return Info.GetAsync(code);
}

同样,我会尽量避免在这样的助手中查询数据库。正如我所说,可以在同一个视图中多次调用helper的方法,因此通常我们有快速方法,或者使用缓存它所需的信息。这与另一个名为“缓存”(caching)的功能有关,该功能在一个简短的回答中有更多内容,您可以了解更多信息。

解决方案

public abstract class CustomView<TModel>  : RazorPage<TModel>
{
    protected IInfo Info => Context.RequestServices.GetRequiredService<IInfo>();

    public Task<string> I(int code)
    {
        return Info.GetAsync(code);
    }
}
public interface IInfo
{
    Task<string> GetAsync(int code);
}
public class Info : IInfo
{
    private readonly ApplicationDbContext context;
    public Info(ApplicationDbContext context)
    {
        this.context = context;
    }

    public async Task<string> GetAsync(int code)
    {
        var myRecord = await context.MyRecords.FindAsync(code);
        if (myRecord != null)
        {
            return myRecord.Info;
        }
        return null;
    }
}
在我的Startup.cs中,我添加了:

services.addScope()


services.AddTransient()也有效。

我尝试过你的方法,但我的目标是:受保护的IInfo Info{get;}=Context.RequestServices.GetRequiredService();我在上下文和错误下看到红色扭曲:字段初始值设定项无法引用非静态、方法或属性“Razor.Page.Context”。@Manu抱歉,请参阅更新。如果您的环境中不支持
=>
,请尝试将其声明为public get private集(而不是像我以前写的那样只读)。现在使用:protected IInfo Info=>Context.RequestServices.GetRequiredService();没有出现错误。但是我仍然需要更多的帮助,比如把HelpInfo放到返回字符串中。在pubic string I(int code)方法中从dbContext获取帮助信息时,我是否正确?否则?@Manu可以插入服务从DbContext查询数据,请参阅我的答案中更新的“我的注释”部分,一般来说,我们应该避免缓慢的查询,以确保性能是可接受的。@Manu是的,实际上您需要的所有数据(尤其是业务数据)都应该提取到视图模型中。这是MVC的严格规则。但是我们通过在页面/视图类中支持右边的数据源(包括对DI的支持),稍微放松了一些。在某些情况下,这可能很方便,但无论如何,数据访问应该很快,我们可以轻松地控制加载到视图模型中的内容,但对于分散在视图中的其他内容,则无法控制。