问题
I'm converting a project to ASP.NET Core. I need to migrate lots of reusable html helpers, but html helpers do not exist in Core.
Some are complex, some simple. Here's a extremely simple example:
@helper EditIcon()
{
<i class="glyphicon glyphicon-pencil"></i>
}
Note that this is only an example.
Point is writing a tag helper for that is gigantic overkill. Same for partials. Same for view components.
We're talking about a little snippet of Razor. What is my best option?
回答1:
So, seems there are only three options:
- tag helpers
- partials
- view components
So no simple way to migrate Razor snippets, without jumping through hoops.
EDIT
So looks like html helpers are available after all. They just haven't been properly documented!
回答2:
@helper directive is removed, but if you may consider using Func<dynamic, IHtmlContent>
you are migrating a legacy code. Here is an example:
@{
Func<dynamic, IHtmlContent> BrowserInfo(string btitle, string href, string imgfilename) =>
@<div style="text-align: center">
<a href="@href">
<img src="~/content/images/browsers/@imgfilename" alt="@btitle"/>@btitle</a>
</div>;
}
And use it just like old helper methods:
@BrowserInfo("Google Chrome", "http://www.google.com/chrome/", "browser_chrome.gif")(null)
来源:https://stackoverflow.com/questions/42494980/migrate-html-helpers-to-asp-net-core