netcore v2-同時處理表單get post請求的方式

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PocoDemo
{

    [Controller]
    public class Home
    {
        [ActionContext]
        public ActionContext Context { get; set; }
        //public IActionResult Today()
        //{
        //    return new ContentResult() { Content= DateTime.Now.ToString("ddd,d MMM") };
        //}

        //public IActionResult Html()
        //{
        //    return new ContentResult()
        //    {
        //        Content="<h1>Hello</h1>",
        //         ContentType="text/html",
        //         StatusCode = 200
        //    };
        //}

        public IActionResult Index([FromServices] IModelMetadataProvider provider)
        {
            var viewdata = new ViewDataDictionary<MyViewModel>(provider, new ModelStateDictionary());

            viewdata.Model = new MyViewModel() { Title = "Hi" };

            return new ViewResult() { ViewName = "index", ViewData = viewdata };

        }

        //同時處理表單get,post請求的方法
        public IActionResult Edit(MyViewModel model)
        {
            var methond = Context.HttpContext.Request.Method;
            switch(methond)
            {
                case "GET":
                    return new ViewResult();
                case "POST":
                    return new ContentResult() { Content= model.Title };
                default:
                    return new ContentResult() {Content= "other methods" };
            }
        }


    }


}
 

發佈了459 篇原創文章 · 獲贊 71 · 訪問量 28萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章