ارتقاء به ASP.NET Core 1.0 - قسمت 10 - بررسی تغییرات Viewها
نویسنده: وحید نصیری
تاریخ: ۱۳۹۵/۰۴/۲۵ ۱۵:۰
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
@using MyProject.Models
Project - Controllers - Models - Services - ViewModels - Views
using System.Collections.Generic;
using Microsoft.AspNetCore.Mvc.Razor;
namespace Core1RtmEmptyTest.StartupCustomizations
{
public class FeatureLocationExpander : IViewLocationExpander
{
public void PopulateValues(ViewLocationExpanderContext context)
{
context.Values["customviewlocation"] = nameof(FeatureLocationExpander);
}
public IEnumerable<string> ExpandViewLocations(
ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
return new[]
{
"/Features/{1}/{0}.cshtml",
"/Features/Shared/{0}.cshtml"
};
}
}
} public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new FeatureLocationExpander());
}); "publishOptions": {
"include": [
"wwwroot",
"Features",
"appsettings.json",
"web.config"
]
},
{
"dependencies": {
//same as before
"Microsoft.Extensions.FileProviders.Embedded": "1.0.0"
}, services.AddMvc();
services.Configure<RazorViewEngineOptions>(options =>
{
options.ViewLocationExpanders.Add(new FeatureLocationExpander());
var thisAssembly = typeof(Startup).GetTypeInfo().Assembly;
options.FileProviders.Clear();
options.FileProviders.Add(new EmbeddedFileProvider(thisAssembly, baseNamespace: "Core1RtmEmptyTest"));
}); "buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true,
"embed": "Features/**/*.cshtml"
},
{
"dependencies": {
// ...
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.VisualStudio.Web.CodeGenerators.Mvc": {
"version": "1.0.0-preview2-final",
"type": "build"
}
},
"tools": {
// ...
"Microsoft.VisualStudio.Web.CodeGeneration.Tools": {
"version": "1.0.0-preview2-final",
"imports": [
"portable-net45+win8"
]
}
}
}
@using Core1RtmTestResources .Web @using Core1RtmTestResources .DomainLayer.Models @using Microsoft.AspNetCore.Identity @using Microsoft.AspNetCore.Mvc.Localization @using Core1RtmTestResources .Web.ViewModels.AccountViewModels //1 Move to Views/Account/_viewImports @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using Core1RtmTestResources .Web.ViewModels.AccountViewModels @using Core1RtmTestResources.Web.ViewModels.AccountViewModels
@inject IHtmlLocalizerFactory HtmlLocalizerFactory
@{
var localizer = HtmlLocalizerFactory.Create(
baseName: "Controllers.TestLocalController" /*مشخصات کنترلر جاری*/,
location: "Core1RtmTestResources.ExternalResources" /*نام اسمبلی ثالث*/);
var sharedLocalizer = HtmlLocalizerFactory.Create(
baseName: "SharedResource" /*مشخصات*/,
location: "Core1RtmTestResources.ExternalResources" /*نام اسمبلی ثالث*/);
} @{
Layout = "_Layout";
} using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc.Localization;
using Microsoft.AspNetCore.Mvc.Razor;
using Microsoft.AspNetCore.Mvc.Razor.Internal;
namespace Core1RtmTestResources.StartupCustomizations
{
public abstract class MyCustomBaseView<TModel> : RazorPage<TModel>
{
//روش خاص تزریق وابستگیها در فایل ویژهی جاری
[RazorInject]
public IHtmlLocalizerFactory MyHtmlLocalizerFactory { get; set; }
public IHtmlLocalizer MySharedLocalizer => MyHtmlLocalizerFactory.Create(
baseName: "SharedResource" /*مشخصات*/,
location: "Core1RtmTestResources.ExternalResources" /*نام اسمبلی ثالث*/);
public bool IsAuthenticated()
{
return Context.User.Identity.IsAuthenticated;
}
#pragma warning disable 1998
public override async Task ExecuteAsync()
{
}
#pragma warning restore 1998
}
} {
"dependencies": {
//same as before
"Microsoft.AspNetCore.Mvc.Razor": "1.0.0"
}
} @using Core1RtmTestResources @using Microsoft.AspNetCore.Mvc.Localization @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @inherits Core1RtmTestResources.StartupCustomizations.MyCustomBaseView<TModel>
MySharedLocalizer from MyCustomBaseView: @MySharedLocalizer["About Title"]
public IEnumerable<string> ExpandViewLocations(ViewLocationExpanderContext context, IEnumerable<string> viewLocations)
{
// {0} - Action Name
// {1} - Controller Name
// {2} - Area Name
if (context.ActionContext.RouteData.Values.TryGetValue("area", out _))
{
return new[]
{
"/Areas/{2}/Features/{1}/{0}.cshtml",
"/Areas/{2}/Features/Shared/{0}.cshtml",
"/Features/Shared/{0}.cshtml"
};
}
else
{
return new[]
{
"/Features/{1}/{0}.cshtml",
"/Features/Shared/{0}.cshtml"
};
}
} <Project Sdk="Microsoft.NET.Sdk.Web">
<ItemGroup>
<EmbeddedResource Include="Features\**\*.cshtml" Exclude="bin\**;obj\**;**\*.xproj;packages\**;@(EmbeddedResource)" />
<None Update="wwwroot\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="1.1.0" />
</ItemGroup>
<ItemGroup>
<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Tools" Version="1.0.0" />
</ItemGroup>
</Project> <None Update="wwwroot\**\*">
<CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
</None> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<MvcRazorCompileOnPublish>false</MvcRazorCompileOnPublish>
</PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<LangVersion>latest</LangVersion>
</PropertyGroup> JetBrains.Annotations
using JetBrains.Annotations;
[assembly: AspMvcViewLocationFormat("~/Features/{1}/{0}.cshtml")] [assembly: AspMvcViewLocationFormat("~/Features/Shared/{0}.cshtml")] [assembly: AspMvcPartialViewLocationFormat("~/Features/{1}/{0}.cshtml")] [assembly: AspMvcPartialViewLocationFormat("~/Features/Shared/{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat("~/Areas/{2}/Features/{1}/{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat("~/Areas/{2}/Features/Shared/{0}.cshtml")]
[assembly: AspMvcAreaViewLocationFormat("~/Features/Shared/{0}.cshtml")]
[assembly: AspMvcAreaPartialViewLocationFormat("~/Areas/{2}/Features/{1}/{0}.cshtml")]
[assembly: AspMvcAreaPartialViewLocationFormat("~/Areas/{2}/Features/Shared/{0}.cshtml")]
[assembly: AspMvcAreaPartialViewLocationFormat("~/Features/Shared/{0}.cshtml")]