카테고리 없음

ASP.NET MVC 3 컨트롤러 추가하기

littlemk 2018. 8. 23. 15:12

프로젝트 생성

<버 전>
- Visual studio 2010 express 

  1. file -> New project 클릭 
  2. MVC 3 Application 클릭 (프로젝트명은 아무거나)
  3. Empty , Razor 클릭 후 확인
  4. 프로젝트 생성 완료
-----------------------------------------------------------------
ASP.NET MVC 3 컨트롤러 추가하기

  1. Controller 폴더 우클릭 -> Add -> Controller 생성 (컨트롤러명 : Home)
        

  1. Index() 부분 소스 변경
    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Test0821.Controllers
{
    public class HomeController : Controller
    {
        //
        // GET: /Home/
       // public ActionResult Index()
        //{
            //return View();
        //}
        public string Index()
        {
            return "안녕하세요~ HomeController 실행했습니다.";
        }
    }
}


  1. 실행화면


  1. ASP.NET 라우팅 시스템 변경하기
    4-1. Global.asax.cs 변경

[Global.asax.cs 파일 화면]



    4-2. controller 명 "Home" -> "Test0821"로 변경

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
namespace Test0821
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode,
    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Test0821", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
        }
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();
            RegisterGlobalFilters(GlobalFilters.Filters);
            RegisterRoutes(RouteTable.Routes);
        }
    }
}


    4-3. Controller 명도 Test0821로 변경

[실행 화면]