• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Asp.netmvc3-JSONpost&AOP

原作者: [db:作者] 来自: [db:来源] 收藏 邀请


1. Ajax call Asp.net MVC action

function save() {
    var data = { 'Name': $('#name').val(), 'Age': $('#age').val() };
    $.ajax({
        url: '/user/Detail', //'@Url.Action("Detail", "user")',
        data: JSON.stringify(data),
        type: 'POST',
        contentType: 'application/json', //'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
            //
        }
    });
}

 

public class UserController : Controller{
    public ActionResult Detail(string Name, string Age){
        User user  = new Usr();
        user.Name = Name;
        user.Age = Age;
        return View("Detail", user);
    }
}

 



summarization:
1. How to post JSON and get
1.1  client passed data type is "json",
    must add --->  contentType: 'application/json; charset=UTF-8', dataType: 'json',
    change the literal object to string using method:     data: JSON.stringify(data),
1.2  MVC recieve the data with
    public ActionResult Detail(string Name, string Age){  // each parameter match the passed json data's each parameter.
    or
    public ActionResult Detail(User user){ // auto ORM.
1.3  passd the data cannot be retrieved by Request.Form.

2. How to post data(application/x-www-form-urlencoded) and get
2.1 if the client doesn't specify contentType, by default, the content type is 'application/x-www-form-urlencoded; charset=UTF-8', the

server can get the data with following, but still cannot b retrieved by Request.Form.
            StreamReader sr = new StreamReader(Request.InputStream, Request.ContentEncoding);
            string m = sr.ReadToEnd();
2.2 How to post data JSON and get with filter. IActionFilter is the AOP implementation in Asp.net Mvc. In filter, get the parameter as

follow.
filterContext.Controller.ValueProvider.GetValue("Name").AttemptedValue;

    [MyAttribute(Message="TestJson")]
    public ActionResult Detail(){....}

    public class MyAttribute : FilterAttribute, IActionFilter
    {
        public string Message { get; set; }
        public void OnActionExecuting(ActionExecutingContext filterContext)   // before executing the method
        {
            filterContext.Controller.ValueProvider.GetValue("Name").AttemptedValue;
            
            filterContext.HttpContext.Response.Write(
            string.Format("[Before Action: {0}]", Message));
        }
        public void OnActionExecuted(ActionExecutedContext filterContext)  // after executed the method
        {
            filterContext.HttpContext.Response.Write(
            string.Format("[After Action: {0}]", Message));
        }
    }

 



3. 1  Request.Form works in asp.net mvc, if the dataType is by default and data is '{a:'1', b:'2'}'.

            $.ajax({
                url: '/user/Detail',
                data: {name: 'Tom'},     // have a test  by enclosing name with single quote if test failed.
                type: 'POST',
                success: function (result) {
                    alert(result);
                },
                error: function (xhRequest, text, thrownError) {
                    alert(xhRequest);  
                }
            });

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
在ASP.NETWebAPI下使用[Serializble]的问题发布时间:2022-07-10
下一篇:
Asp.NetMVC4Bundle捆绑压缩技术发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap