netcore v2-將操作映射到方法的兩種方式

/****************************poco控制器********************************/

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
    {

        //第一種 按名稱映射
        [NonAction]
        public IActionResult About()
        {
            return new ContentResult() { Content = "about" };
        }

        
        [ActionName("About")]
        public IActionResult LoveGermanStepherds()
        {
            return new ContentResult() { Content = "about" };

        }

        //第二種 按照http動詞映射
        //[AcceptVerbs("post")]
        //public IActionResult CallMe()
        //{
        //    return new ContentResult() { Content = "callme" };
        //}

        //[AcceptVerbs("post","get")]
        //public IActionResult CallMe()
        //{
        //    return new ContentResult() { Content = "callme" };
        //}

        [HttpGet]
        [HttpPost]
        public IActionResult CallMe()
        {
            return new ContentResult() { Content = "callme" };
        }


    }


}

/************************index.cshtml********************************/


@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@
    <h1>@ViewData.Model.Title</h1>
    <form action="/home/callme" target="_self" method="post">
        <input type="text" name="name"  value="zhangsan"/>
        <button type="submit" >submit</button>
    </form>
@{
}
 

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