In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces how to automatically bind request parameters to ASPX, ASHX and MVC in .NET, which has a certain reference value. Interested friends can refer to it. I hope you will learn a lot after reading this article.
Preface
When you start an AJAX application, you often have to parse the parameters passed by the client manually, which is extremely boring, and the code is full of code like Request ["xxx"].
The purpose of this article is to show beginners how to automatically bind parameters sent by the client with AJAX to strongly typed member properties or method parameters.
Automatically bind to ASPX and ASHX
Framework support
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Namespace Happy.Web
{
Public interface IWantAutoBindProperty
{
}
}
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Namespace Happy.Web
{
[AttributeUsage (AttributeTargets.Property, AllowMultiple = true)]
Public sealed class AutoBind: Attribute
{
}
}
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Using System.Web
Using Newtonsoft.Json
Using Happy.ExtensionMethods.Reflection
Namespace Happy.Web
{
Public class JsonBinderModule: IHttpModule
{
Public void Init (HttpApplication context)
{
Context.PreRequestHandlerExecute + = OnPreRequestHandlerExecute
}
Private void OnPreRequestHandlerExecute (object sender, EventArgs e)
{
If (! (HttpContext.Current.CurrentHandler is IWantAutoBindProperty))
{
Return
}
Var properties = HttpContext.Current.CurrentHandler.GetType () .GetProperties ()
Foreach (var property in properties)
{
If (! property.IsDefined (typeof (AutoBind), true))
{
Continue
}
String json = HttpContext.Current.Request [property.Name]
Var value = JsonConvert.DeserializeObject (json, property.PropertyType)
Property.SetValue (HttpContext.Current.Handler, value)
}
}
Public void Dispose ()
{
}
}
}
Code example
The copy code is as follows:
The copy code is as follows:
/ / /
Var data = {
Name: 'Duan Guangwei'
Age: 28
}
Ext.Ajax.request ({
Url:'.. / handlers/JsonBinderTest.ashx'
Method: 'POST'
Params: {user: Ext.encode (data)}
});
The copy code is as follows:
Using System
Using System.Web
Using Happy.Web
Public class JsonBinderTest: IHttpHandler, IWantAutoBindProperty
{
[AutoBind]
Public User user {get; set;}
Public void ProcessRequest (HttpContext context)
{
Context.Response.ContentType = "text/plain"
Context.Response.Write ("name: {0}, age: {1}", user.Name, user.Age))
}
Public bool IsReusable
{
Get
{
Return false
}
}
}
Public class User
{
Public string Name {get; set;}
Public int Age {get; set;}
}
Automatically bind to MVC
Framework support
The copy code is as follows:
Using System
Using System.Collections.Generic
Using System.Linq
Using System.Text
Using System.Threading.Tasks
Using System.Web.Mvc
Using Newtonsoft.Json
Namespace Tenoner.Web.Mvc
{
Public class JsonBinder: IModelBinder
{
Public object BindModel (ControllerContext controllerContext, ModelBindingContext bindingContext)
{
String json = controllerContext.HttpContext.Request [bindingContext.ModelName]
Return JsonConvert.DeserializeObject (json, bindingContext.ModelType)
}
}
}
Thank you for reading this article carefully. I hope the article "how to automatically bind request parameters to ASPX, ASHX and MVC in .NET" shared by the editor will be helpful to you. At the same time, I hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.