學習時使用的是VS2017+Core2.1了,不再討論VS2015和core1.1的東西。
配置ASP.NET Core Web應用程序以使用Telerik UI for ASP.NET Core:
第一步:創建ASP.NET Core Web應用程序。
第二步:添加Kendo UI NuGet包。
因為我們是本地安裝,所以建立一個源
默認源路徑(以安裝路徑為准):C:\Program Files (x86)\Progress\Telerik UI for ASP.NET Core R2 2018\wrappers\aspnetcore\Binaries\AspNet.Core
其實就做了一件事,就是在.csproj
文件中添加了一行引用代碼,當然你可以不用以上操作,直接在該文件中添加一下代碼也是可以的。
.csproj
文件的打開方法
修改Startup.cs文件
修改ConfigureServices方法
public void ConfigureServices(IServiceCollection services) { services.Configure<CookiePolicyOptions>(options => { // This lambda determines whether user consent for non-essential cookies is needed for a given request. options.CheckConsentNeeded = context => true; options.MinimumSameSitePolicy = SameSiteMode.None; }); services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1) .AddJsonOptions(options => options.SerializerSettings.ContractResolver = new DefaultContractResolver()); // Add Kendo UI services to the services container services.AddKendo(); }
需要添加引用:using Newtonsoft.Json.Serialization;
修改Configure方法(這個方法自2018R2開始過時了,可以不用寫)
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { ... // Configure Kendo UI app.UseKendo(env); }
將 Kendo.Mvc.UI
命名空間導入 文件~/Views/_ViewImports.cshtml
,使用指令:@using Kendo.Mvc.UI
.
@using KendoUiTest @using KendoUiTest.Models @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers @addTagHelper *, Kendo.Mvc @using Kendo.Mvc.UI
復制Kendo UI客戶端資源。
手動安裝 - 要手動安裝資源,請將teles.ui.for.aspnet mvc安裝目錄中的js和styles
夾復制到wwwroot \ lib \ kendo-ui。
我個人更覺得再加一級版本號目錄就好了
方便以后手動升級,畢竟肯定要手動升級,新版本放在新目錄里,方便測試回滾吧,比較土的方法。
在〜/ Views / Shared / _Layout.cshtml中注冊Kendo UI樣式和腳本。
重要說明:在默認的.NET Core模板中,jQuery腳本包含在<body>元素的末尾。 要正確加載ASP.NET HtmlHelpers的Telerik UI,請將jQuery腳本和Kendo UI客戶端腳本移動到<head>元素,並確保在jQuery之后加載Kendo UI腳本。
_Layout.cshtml文件
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>@ViewData["Title"] - KendoUiTest</title> <environment include="Development"> <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" /> <link rel="stylesheet" href="~/css/site.css" /> <link rel="stylesheet" href="~/lib/kendo-ui/2018.2.260/styles/kendo.common-nova.min.css" /> <link rel="stylesheet" href="~/lib/kendo-ui/2018.2.260/styles/kendo.nova.min.css" /> </environment> <environment exclude="Development"> <link rel="stylesheet" href="https://ajax.aspnetcdn.com/ajax/bootstrap/3.3.7/css/bootstrap.min.css" asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" /> <link rel="stylesheet" href="~/css/site.min.css" asp-append-version="true" /> <link rel="stylesheet" href="~/lib/kendo-ui/2018.2.260/styles/kendo.common-nova.min.css" asp-fallback-href="~/lib/kendo-ui/2018.2.260/styles/kendo.common-nova.min.css" asp-fallback-test-class="k-common-test-class" asp-fallback-test-property="opacity" asp-fallback-test-value="0" /> <link rel="stylesheet" href="~/lib/kendo-ui/2018.2.260/styles/kendo.nova.min.css" asp-fallback-href="~/lib/kendo-ui/2018.2.260/styles/kendo.nova.min.css" asp-fallback-test-class="k-theme-test-class" asp-fallback-test-property="opacity" asp-fallback-test-value="0" /> </environment> <environment include="Development"> <script src="~/lib/jquery/dist/jquery.js"></script> @* Place Kendo UI scripts after jQuery *@ <script src="~/lib/kendo-ui/2018.2.260/js/kendo.all.min.js"></script> <script src="~/lib/kendo-ui/2018.2.260/js/kendo.aspnetmvc.min.js"></script> </environment> <environment exclude="Development"> ... <script src="https://ajax.aspnetcdn.com/ajax/jquery/jquery-2.2.0.min.js" asp-fallback-src="~/lib/jquery/dist/jquery.min.js" asp-fallback-test="window.jQuery" crossorigin="anonymous" integrity="sha384-K+ctZQ+LL8q6tP7I94W+qzQsfRV2a+AfHIi9k8z8l9ggpc8X+Ytst4yBo/hH+8Fk"> </script> @* Place Kendo UI scripts after jQuery *@ <script src="~/lib/kendo-ui/2018.2.260/js/kendo.all.min.js" asp-fallback-src="~/lib/kendo-ui/2018.2.260/js/kendo.all.min.js" asp-fallback-test="window.kendo"> </script> <script src="~/lib/kendo-ui/2018.2.260/js/kendo.aspnetmvc.min.js" asp-fallback-src="~/lib/kendo-ui/2018.2.260/js/kendo.aspnetmvc.min.js" asp-fallback-test="kendo.data.transports['aspnetmvc-ajax']"> </script> </environment> </head> <body> @RenderBody() @RenderSection("Scripts", required: false) </body> </html>
通過將以下示例中的代碼段添加到〜/ Views / Home / Index.cshtml,演示使用Kendo UI DatePicker組件。
Index.cshtml文件(替換掉默認的代碼,否則可能導致樣式或js不起作用)
@{ ViewData["Title"] = "Home Page"; } <h2>Kendo UI DatePicker</h2> @(Html.Kendo().DatePicker() .Name("datepicker") )
運行網站,查看效果
測試完成。
關於:Copy Kendo UI Client Resources through NPM and Webpack(通過NPM和Webpack復制Kendo UI客戶端資源)
請移步原網站查看:https://docs.telerik.com/aspnet-core/getting-started/getting-started