Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set up the website in the Development of ASP.NET MVC5 website

2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

ASP.NET MVC5 website development how to set up the site, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

The website configuration is generally used to save some settings of the website, and it is more appropriate to write in the configuration file than in the database, because the configuration file itself has a cache and reads it into the cache with the website startup, which is faster. Saving in the database to create a table for a separate record is not clear enough, and reading and writing is not as easy as the configuration file. What we need to do this time is the basic information of the website, and the data is saved in SiteConfig.config.

I. website configuration class (SiteConfig)

1. Create a new folder Config in the Nninesky.Core project

2. Add class SiteConfig to the Config folder.

Using System.ComponentModel.DataAnnotations;using System.Configuration;namespace Ninesky.Core.Config {/ website configuration class / 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 }} / website name / [Required (ErrorMessage = "*")] [StringLength (50, ErrorMessage = "up to {1} characters")] [Display (Name = "site name")] public string SiteName {get {return keyValues ["SiteName"] = = null? String.Empty: keyValues ["SiteName"] .value;} set {keyValues ["SiteName"]. Value = value;}} / [Required (ErrorMessage = "*")] [StringLength (50, ErrorMessage = "up to {1} characters")] [Display (Name = "site title")] 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 = "up to {1} characters")] [Display (Name = "website address")] public string SiteUrl {get {return keyValues ["SiteUrl"] = null? "http://": keyValues [" SiteUrl "] .value;} set {keyValues [" SiteUrl "]. Value = value;}} / Meta keyword / [DataType (DataType.MultilineText)] [StringLength (500, ErrorMessage =" up to {1} characters ")] [Display (Name =" Meta keyword ")] public string MetaKeywords {get {return keyValues [" MetaKeywords "] = null? String.Empty: keyValues ["MetaKeywords"] .value;} set {keyValues ["MetaKeywords"]. Value = value;}} / Meta description / [DataType (DataType.MultilineText)] [StringLength (1000, ErrorMessage = "up to {1} characters")] [Display (Name = "Meta description")] public string MetaDescription {get {return keyValues ["MetaDescription"] = null? String.Empty: keyValues ["MetaDescription"] .value;} set {keyValues ["MetaDescription"]. Value = value;}} / DataType (DataType.MultilineText)] [StringLength (1000, ErrorMessage = "up to {1} characters")] [Display (Name = "copyright information")] public string Copyright {get {return keyValues ["Copyright"] = = null? "Ninesky copyright": keyValues ["Copyright"] .value;} set {keyValues ["Copyright"] .Value = value;}}

The Siteconfig class inherits from ConfigurationSection, and only the configuration section can be read and written from this class.

Declare a child element of a configuration element in the class, private static ConfigurationProperty _ property, whose configuration entity type is KeyValueConfigurationCollection (key / value collection).

The copy code is as follows:

Private static ConfigurationProperty _ property = new ConfigurationProperty (string.Empty, typeof (KeyValueConfigurationCollection), null, ConfigurationPropertyOptions.IsDefaultCollection)

Xu then declares a property private KeyValueConfigurationCollection keyValues in the class. Use keyValues to get and set the collection of configuration keys / values.

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

You can then use keyValues ["name"] to get the specific configuration.

/ website name / / [Required (ErrorMessage = "*")] [StringLength (50, ErrorMessage = "up to {1} characters")] [Display (Name = "site name")] public string SiteName {get {return keyValues ["SiteName"] = = null? String.Empty: keyValues ["SiteName"] .value;} set {keyValues ["SiteName"] .Value = value;}}

Whether it looks like other model classes or not, the knowledge Get;Set; is different.

Set the type and path of the configuration file

Open the web.config file of the Nniesky.web project, find configSections, and add the SiteConfig configuration section

The red box is the added type, indicating the name and type of the configuration section. Pay attention to the red line. RestartOnExternalChanges is set to "false". If not, the website will be restarted after the configuration file is modified.

Add the path to the profile at the end of the configuration file

The red box in the picture shows the added content, indicating that the location file of SiteConfig is the file named SiteConfig.config under the website directory Config folder.

Then add the Config folder to the project, and then add a configuration file named SiteConfig.config.

The key name in the configuration file corresponds to the property name of the SiteConfig.

III. Controllers and views

1. Read the configuration file

In Ninesky.Web/Areas/Control/Controllers [right]-> add-> Controller, enter the controller name ConfigController.

Add a method SiteConfig method to the control

/ site settings / public ActionResult SiteConfig () {SiteConfig _ siteConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration ("~") .GetSection ("SiteConfig") as Ninesky.Core.Config.SiteConfig; return View (_ siteConfig);}

The code is simple, and the configuration information is read out using WebConfigurationManager's GetSection method.

Right-click to add a view to display a property.

@ model Ninesky.Core.Config.SiteConfig@ {ViewBag.Title = "site Settings" } @ section SideNav {@ Html.Partial ("SideNavPartialView")} @ Html.ActionLink ("Home", "Index", "Home") @ Html.ActionLink ("system Settings", "Index") site Settings @ 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. Save the configuration file.

Add another SiteConfig method of type [HttpPost] to the controller.

[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 = "modified successfully", Message = "site settings modified successfully", Buttons = new List {"return"}}) } else return View (_ siteConfig);}}

The code is also very simple, just like reading the configuration file, using the GetSection method of WebConfigurationManager to read the configuration information into _ siteConfig, and then binding the information submitted by the view with TryUpdateModel (_ siteConfig).

If the binding is successful, use the _ siteConfig.CurrentConfiguration.Save () method to save the configuration information (this method inherits from ConfigurationSection and does not have to be implemented by yourself).

The effect is as follows

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report