ASP.NET MVC5网站开发之网站设置(九)

2019-05-22 10:45:45于丽

1、配置文件的读取

在Ninesky.Web/Areas/Control/Controllers【右键】->添加->控制器,输入控制器名ConfigController。 

在控制其中添加方法SiteConfig方法 

/// <summary>
 /// 站点设置
 /// </summary>
 /// <returns></returns>
 public ActionResult SiteConfig()
 {
 SiteConfig _siteConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("SiteConfig") as Ninesky.Core.Config.SiteConfig;
 return View(_siteConfig);
 }

代码很简单,利用WebConfigurationManager的GetSection方法就将配置信息读出来了。 

右键添加视图,将个属性显示出来。 

@model Ninesky.Core.Config.SiteConfig

@{
 ViewBag.Title = "站点设置";
}

@section SideNav{@Html.Partial("SideNavPartialView")}

<ol class="breadcrumb">
 <li><span class="glyphicon glyphicon-home"></span> @Html.ActionLink("首页", "Index", "Home")</li>
 <li>@Html.ActionLink("系统设置", "Index")</li>
 <li class="active">站点设置</li>
</ol>

@using (Html.BeginForm())
{
 @Html.AntiForgeryToken()
 
 <div class="form-horizontal">
 @Html.ValidationSummary(true, "", new { @class = "text-danger" })

 <div class="form-group">
 @Html.LabelFor(model => model.SiteName, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.SiteName, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.SiteTitle, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.SiteTitle, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.SiteTitle, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.SiteUrl, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.SiteUrl, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.SiteUrl, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.MetaKeywords, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.MetaKeywords, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.MetaKeywords, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.MetaDescription, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.MetaDescription, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.MetaDescription, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 @Html.LabelFor(model => model.Copyright, htmlAttributes: new { @class = "control-label col-md-2" })
 <div class="col-md-10">
 @Html.EditorFor(model => model.Copyright, new { htmlAttributes = new { @class = "form-control" } })
 @Html.ValidationMessageFor(model => model.Copyright, "", new { @class = "text-danger" })
 </div>
 </div>

 <div class="form-group">
 <div class="col-md-offset-2 col-md-10">
 <input type="submit" value="保存" class="btn btn-default" />
 </div>
 </div>
 </div>
}