如何添加所有 Razor 页面都可以访问的功能?
How can I add a function that all Razor Pages can access?
使用 Razor Pages ASP.Net 核心,我有一些我想在每个页面上使用的功能。我想这曾经是用 App_Code 完成的,但在 Core 中似乎不再有效。我如何在 Asp.Net Core Razor Pages 中完成此操作?
选项 1 - DI
1 - 创建具有相关功能的服务 class
public class FullNameService
{
public string GetFullName(string first, string last)
{
return $"{first} {last}";
}
}
2-在启动时注册服务
services.AddTransient<FullNameService>();
3- 将其注入剃须刀页面
public class IndexModel : PageModel
{
private readonly FullNameService _service;
public IndexModel(FullNameService service)
{
_service = service;
}
public string OnGet(string name, string lastName)
{
return _service.GetFullName(name, lastName);
}
}
选项 2 - Base Model
1- 使用函数
创建一个基本页面模型
public class BasePageModel : PageModel
{
public string GetFullName(string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从基本模型派生其他页面
public class IndexModel : BasePageModel
{
public string OnGet(string first, string lastName)
{
return GetFullName(first, lastName);
}
}
选项 3 - Static Class
1- 使用可从所有页面访问的静态函数
public static class FullNameBuilder
{
public static string GetFullName(string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从剃刀页面调用静态函数
public class IndexModel : PageModel
{
public string OnGet(string first, string lastName)
{
return FullNameBuilder.GetFullName(first, lastName);
}
}
选项 4 - Extension Methods
1- 为特定类型的对象(例如字符串)创建扩展方法
public static class FullNameExtensions
{
public static string GetFullName(this string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从剃须刀页面调用分机
public class IndexModel : PageModel
{
public string OnGet(string first, string lastName)
{
return first.GetFullName(lastName);
}
}
使用 Razor Pages ASP.Net 核心,我有一些我想在每个页面上使用的功能。我想这曾经是用 App_Code 完成的,但在 Core 中似乎不再有效。我如何在 Asp.Net Core Razor Pages 中完成此操作?
选项 1 - DI
1 - 创建具有相关功能的服务 class
public class FullNameService
{
public string GetFullName(string first, string last)
{
return $"{first} {last}";
}
}
2-在启动时注册服务
services.AddTransient<FullNameService>();
3- 将其注入剃须刀页面
public class IndexModel : PageModel
{
private readonly FullNameService _service;
public IndexModel(FullNameService service)
{
_service = service;
}
public string OnGet(string name, string lastName)
{
return _service.GetFullName(name, lastName);
}
}
选项 2 - Base Model
1- 使用函数
创建一个基本页面模型public class BasePageModel : PageModel
{
public string GetFullName(string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从基本模型派生其他页面
public class IndexModel : BasePageModel
{
public string OnGet(string first, string lastName)
{
return GetFullName(first, lastName);
}
}
选项 3 - Static Class
1- 使用可从所有页面访问的静态函数
public static class FullNameBuilder
{
public static string GetFullName(string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从剃刀页面调用静态函数
public class IndexModel : PageModel
{
public string OnGet(string first, string lastName)
{
return FullNameBuilder.GetFullName(first, lastName);
}
}
选项 4 - Extension Methods
1- 为特定类型的对象(例如字符串)创建扩展方法
public static class FullNameExtensions
{
public static string GetFullName(this string first, string lastName)
{
return $"{first} {lastName}";
}
}
2- 从剃须刀页面调用分机
public class IndexModel : PageModel
{
public string OnGet(string first, string lastName)
{
return first.GetFullName(lastName);
}
}