千家信息网

ASP.NET MVC5网站开发中如何进行网站设置

发表于:2026-11-02 作者:千家信息网编辑
千家信息网最后更新 2026年11月02日,ASP.NET MVC5网站开发中如何进行网站设置,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。网站配置一般用来保存网站
千家信息网最后更新 2026年11月02日ASP.NET MVC5网站开发中如何进行网站设置

ASP.NET MVC5网站开发中如何进行网站设置,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

网站配置一般用来保存网站的一些设置,写在配置文件中比写在数据库中要合适一下,因为配置文件本身带有缓存,随网站启动读入缓存中,速度更快,而保存在数据库中要单独为一条记录创建一个表,结构不够清晰,而且读写也没有配置文件容易实现。这次要做的是网站的基本信息,数据保存在SiteConfig.config。

一、网站配置类(SiteConfig)

1、在Nninesky.Core项目新建文件夹Config

2、在Config文件夹添加类SiteConfig。

using System.ComponentModel.DataAnnotations;using System.Configuration;namespace Ninesky.Core.Config{ ///  /// 网站配置类 ///  public class SiteConfig : ConfigurationSection { private static ConfigurationProperty _property = new ConfigurationProperty(string.Empty, typeof(KeyValueConfigurationCollection), null, ConfigurationPropertyOptions.IsDefaultCollection); [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)] private KeyValueConfigurationCollection keyValues { get { return (KeyValueConfigurationCollection)base[_property]; } set { base[_property] = value; } } ///  ///网站名称 ///  [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "最多{1}个字符")] [Display(Name = "网站名称")] public string SiteName { get { return keyValues["SiteName"] == null? string.Empty: keyValues["SiteName"].Value; } set { keyValues["SiteName"].Value = value; } } ///  ///网站标题 ///  [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "最多{1}个字符")] [Display(Name = "网站标题")] public string SiteTitle { get { return keyValues["SiteTitle"] == null? string.Empty: keyValues["SiteTitle"].Value; } set { keyValues["SiteTitle"].Value = value; } } ///  ///网站地址 ///  [DataType(DataType.Url)] [Required(ErrorMessage = "*")] [StringLength(500, ErrorMessage = "最多{1}个字符")] [Display(Name = "网站地址")] public string SiteUrl { get { return keyValues["SiteUrl"] == null ? "http://" : keyValues["SiteUrl"].Value; } set { keyValues["SiteUrl"].Value = value; } } ///  ///Meta关键词 ///  [DataType(DataType.MultilineText)] [StringLength(500, ErrorMessage = "最多{1}个字符")] [Display(Name = "Meta关键词")] public string MetaKeywords { get { return keyValues["MetaKeywords"] == null ? string.Empty: keyValues["MetaKeywords"].Value; } set { keyValues["MetaKeywords"].Value = value; } } ///  ///Meta描述 ///  [DataType(DataType.MultilineText)] [StringLength(1000, ErrorMessage = "最多{1}个字符")] [Display(Name = "Meta描述")] public string MetaDescription { get { return keyValues["MetaDescription"] == null ? string.Empty : keyValues["MetaDescription"].Value; } set { keyValues["MetaDescription"].Value = value; } } ///  ///版权信息 ///  [DataType(DataType.MultilineText)] [StringLength(1000, ErrorMessage = "最多{1}个字符")] [Display(Name = "版权信息")] public string Copyright { get { return keyValues["Copyright"] == null ? "Ninesky 版权所有" : keyValues["Copyright"].Value; } set { keyValues["Copyright"].Value = value; } } }}

Siteconfig类继承自ConfigurationSection,继承自这个类是才能读写配置节。

在类中声明一个配置元素的子元素 private static ConfigurationProperty _property,子元素的配置实体类型是KeyValueConfigurationCollection(键/值集合)。

复制代码 代码如下:

private static ConfigurationProperty _property = new ConfigurationProperty(string.Empty, typeof(KeyValueConfigurationCollection), null, ConfigurationPropertyOptions.IsDefaultCollection);


然后徐再在类中声明一个属性private KeyValueConfigurationCollection keyValues。利用keyValues获取、设置配置节键/值集合。

 [ConfigurationProperty("", Options = ConfigurationPropertyOptions.IsDefaultCollection)] private KeyValueConfigurationCollection keyValues { get { return (KeyValueConfigurationCollection)base[_property]; } set { base[_property] = value; } }

然后就可以使用keyValues["name"]获取设置具体配置了。

///  ///网站名称 ///  [Required(ErrorMessage = "*")] [StringLength(50, ErrorMessage = "最多{1}个字符")] [Display(Name = "网站名称")] public string SiteName { get { return keyValues["SiteName"] == null? string.Empty: keyValues["SiteName"].Value; } set { keyValues["SiteName"].Value = value; } }


看起来是不是跟其他模型类差不多,知识Get;Set;有所不同。

二、设置配置文件的类型和路径

打开Nniesky.web项目的 web.config文件,找到configSections,然后添加SiteConfig配置节

红框部分为添加类型,说明了配置节的名称和类型,注意红线部分,restartOnExternalChanges设为"false",如果不设置,配置文件修改后会重启网站。

在配置文件的结尾添加配置文件的路径

图中红框部分为添加内容,指明SiteConfig的位置文件在网站目录Config文件夹下名为SiteConfig.config的文件。

然后在项目中添加Config文件夹,然后添加名为SiteConfig.config的配置文件。

      

配置文件中的键名与SiteConfig的属性名对应。

三、控制器和视图
1、配置文件的读取

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

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

///  /// 站点设置 ///  ///  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")}@using (Html.BeginForm()){ @Html.AntiForgeryToken()  
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.SiteName, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.SiteName, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SiteName, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.SiteTitle, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.SiteTitle, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SiteTitle, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.SiteUrl, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.SiteUrl, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.SiteUrl, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.MetaKeywords, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.MetaKeywords, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.MetaKeywords, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.MetaDescription, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.MetaDescription, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.MetaDescription, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Copyright, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Copyright, new { htmlAttributes = new { @class = "form-control" } }) @Html.ValidationMessageFor(model => model.Copyright, "", new { @class = "text-danger" })
}

2、配置文件的保存。

在控制器中再添加一个[HttpPost]类型的SiteConfig方法。

[ValidateInput(false)] [ValidateAntiForgeryToken] [HttpPost] public ActionResult SiteConfig(FormCollection form) { SiteConfig _siteConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~").GetSection("SiteConfig") as Ninesky.Core.Config.SiteConfig; if (TryUpdateModel(_siteConfig)) { _siteConfig.CurrentConfiguration.Save(); return View("Prompt", new Prompt() { Title = "修改成功", Message = "成功修改了网站设置", Buttons = new List { "返回" } }); } else return View(_siteConfig); } }

代码也非常简单,与读取配置文件相同,使用WebConfigurationManager的GetSection方法将配置信息读入_siteConfig中,然后用TryUpdateModel(_siteConfig)绑定视图提交过来的信息。

如果绑定成功,利用_siteConfig.CurrentConfiguration.Save()方法保存配置信息(这个方法继承自ConfigurationSection,不用自己实现)。

效果如下图

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对的支持。

配置 网站 文件 信息 字符 方法 名称 类型 控制 代码 控制器 文件夹 成功 元素 属性 数据 版权 站点 视图 项目 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 上海挚品互联网科技有限公司 互联网科技工作委员会毛军 礼当家互联网科技 华中科技大学互联网 丰台科技园 互联网 伴你行互联网科技有限公司 互联网科技公司有哪些 左右互联网络科技 广州慧亚互联网科技怎么样 湖北码尼互联网科技有限公司电话 速维有品互联网信息科技(苏州)有限公司 高科技互联网发展前景如何 黑科技 体验场景 互联网大会 伍佰万互联网科技怎么赚钱 美国互联网科技公司 半导体是互联网高科技吗 广州燕处互联网科技有限公司 易租星互联网科技有限公司 山东蛋芒互联网科技有限公司 黑龙江互联网科技公司 互联网科技网 鸠鸠互联网科技 互联网科技战争 蓝色互联网科技 互联网经济与金融科技 南平创想时代互联网科技有限公司 科技互联网海报 锡山区互联网智能科技产品供应 陕西云淼互联网科技公司 普陀区互联网教育科技管理办法
0