1. New Project MVC Application c#
  2. Right Click Controllers ->Add->Controller->Student
  3. Add View

ใส่โค้ดตามตัวอย่าง

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcApplication1.Controllers
{
    public class StudentController : Controller
    {
        //
        // GET: /Student/

        public ActionResult Index()
        {
            return View();
        }
        public String Add()
        {

            return "Hello from Student-Add";
        }
        public ActionResult arPartialView()
        {
            return PartialView("PartialView1");
        }
        public ActionResult arRedirect()
        {
            return Redirect("http://www.google.co.th");
        }
        public ActionResult arRedirectToAction()
        {
            return RedirectToAction("Index");
        }
        public ActionResult arJason()
        {
            //not support cross domain
            List<String> a = new List<String>()
            {
                "a","b","c"
            };
            return Json(a, JsonRequestBehavior.AllowGet);
        }
        public ActionResult arContent()
        {
           // return Content("<script>alert('Hello');</script>", "application/javascript");// content type
            return Content("<script>alert('Hello');</script>");
        }
        public ActionResult arJavaScript()
        {
            return JavaScript("alert('Hello')");
        }
        public ActionResult arResultDictToRoute()
        {
            // ข้าม controller
            return RedirectToRoute("default",
                new { Controller = "Home", Action = "index" });
        }
        public ActionResult arFile()
        {
            return File("C:\\Users\\Public\\Pictures\\Sample Pictures\\Penguins.jpg", "image/jpeg");
        }
        public String Delete(String StudentID)
        {
            return "Delete Student white ID=" + StudentID;
            //run http://localhost:1048/student/delete?studentid=11&id=5050  //auto parameter from query string

        }
        //http://localhost:1048/student/update/1
        public String Update(Int32 id)// name id only 
        {
            //if get keyid ex. http://localhost:1048/student/update/1
            //only 1 parameter 
            return "Update Student with ID=" + id;
        }

        //http://localhost:1048/Modify/2012/12345
        public String Update2(String Year,String StudentID) {

            //return "Update Student: Year=" + Year + ",ID=" + StudentID;
            String r1 = "Update Student: Year=" + Year + ",ID=" + StudentID;
            String r2 = "Update Student: Year=" + Year;
            if (StudentID == null)
            {
                return r2;
            }
            else
            {
                return r1;
            }

        }

    }
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace MvcApplication1
{
    public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Student",
                "student/modify/{year}/{studentid}",
                new {studentid=UrlParameter.Optional,//หมายถึงไม่มีก็ได้
                   // year=UrlParameter.Optional,
                    Controller="Student",Action="Update2" }
                //year is optional ถ้าต้องการให้ Action อยู่บน parameter  ให้เพิ่ม   "student/{action}/{year}/{studentid}"
                //แล้วลบ Action="2" ออก
                );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

ตรง map route สามารถเพิ่มเงื่อนไขในการตรวจสอบค่าที่รับมาได้โดยแก้ไขในไฟล์

  routes.MapRoute(
                "Student",
                "student/modify/{year}/{studentid}",
                new
                {
                    studentid = UrlParameter.Optional,//หมายถึงไม่มีก็ได้
                    // year=UrlParameter.Optional,
                    Controller = "Student",
                    Action = "Update2"
                },
                    new  { year=@"\d{4}",studentid=@"\d{7}"} //Map Expression decimal number

 

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.