수색…


소개

T4MVC 는 마술 문자열과 달리 MVC 라우팅 메커니즘에서 사용하기 위해 강력한 형식의 도우미를 생성하는 T4 템플릿 입니다. T4MVC는 다양한 컨트롤러, 동작 및 뷰를 감지하고 해당 뷰에 대한 참조를 작성하여 뷰를 라우트하거나 액세스하려는 시도가 유효하지 않은 경우 컴파일 타임 오류를 발생시킵니다.

액션 호출

MVC에는 링크, 양식 작업 또는 작업으로 리디렉션을 위해 라우팅 용도로 작업을 지정하려는 몇 가지 시나리오가 있습니다. MVC 네임 스페이스를 통해 작업을 지정할 수 있습니다.

HomeController 와 같은 컨트롤러가 주어 졌을 때 :

public class HomeController : Controller
{
    public ActionResult Index()
    {
        ...
    }
    public ActionResult MyAction()
    {
        ...
    }
    public ActionResult MyActionWithParameter(int parameter)
    {
        ...
    }
}

T4MVC는 액션을 오버라이드하는 상속 된 컨트롤러를 생성합니다. 이 재정의는 경로 데이터를 올바르게 설정하여 MVC의 UrlHelper 가 적절한 URL을 생성하도록합니다. 이 메서드를 호출하여 UrlHelper 의 다양한 메서드에 UrlHelper 있습니다. 아래 예제는 기본 MVC 경로가 사용된다고 가정합니다.

링크

지정된 텍스트가 a 태그를 생성하려면 다음을 수행하십시오.

@Html.ActionLink("Link Text", MVC.Home.Index() )
//result: <a href="/">Link Text</a>
@Html.ActionLink("Link Text", MVC.Home.MyAction() )
//result: <a href="/Home/MyAction">Link Text</a>
//T4MVC also allows you to specify the parameter without creating an anonymous object:
@Html.ActionLink("Link Text", MVC.Home.MyActionWithParameter(1) )
//result: <a href="/Home/MyActionWithParameter/1">Link Text</a>

URL을 생성하려면 다음과 같이하십시오.

@Url.Action( MVC.Home.Index() )
//result: /
@Url.Action("Link Text", MVC.Home.MyAction() )
//result: /Home/MyAction
@Url.Action("Link Text", MVC.Home.MyActionWithParameter(1) )
//result: /Home/MyActionWithParameter/1

T4MVC는 MVC 라우팅과 같은 규칙을 따른다 것을 알 - 있도록이 기본 경로 변수를 지정하지 않을 Index 상의 조치 HomeController 생성하지 않습니다 /Home/Index 만의 대신에 완벽하게 유효하고 축약 된 형태 / .

폼 메서드

올바른 action 지정된 form 태그를 생성하려면 다음을 action 하십시오.

@Html.BeginForm( MVC.Home.Index(), FormMethod.Get /* or FormMethod.Post */ )
{
    //my form
}
//result:
<form action="/" method="GET">
    //my form
</form>
@Html.BeginForm( MVC.Home.MyActionWithParameter(1), FormMethod.Get /* or FormMethod.Post */ )
{
    //my form
}
//result:
<form action="/Home/MyActionWithParameter/1" method="GET">
    //my form
</form>

액션으로 리디렉션

컨트롤러에있는 경우 현재 동작의 동작으로 리디렉션 할 수 있습니다. 이것은 할 수있다, likeso :

public class RedirectingController : Controller
{
    public ActionResult MyRedirectAction()
    {
        ...
        return RedirectToAction( MVC.Redirecting.ActionToRedirectTo() );
        //redirects the user to the action below.
    }
    public ActionResult ActionToRedirectTo()
    {
        ...
    }
}


Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow