2013-01-24 3 views
2

ASP.NET MVC 4 앱에서 로그인 양식을 설정하려고합니다. 다음과 같이 현재, 내보기를 구성한 :ASP.NET MVC 4의 GET 및 POST 경로 라우팅

RouteConfig.cs

routes.MapRoute(
    "DesktopLogin", 
    "{controller}/account/login", 
    new { controller = "My", action = "Login" } 
); 

MyController.cs

public ActionResult Login() 
{ 
    return View("~/Views/Account/Login.cshtml"); 
} 

[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public ActionResult Login(LoginModel model) 
{ 
    return View("~/Views/Account/Login.cshtml"); 
} 

난에/계정/로그인을 방문 할 때 브라우저에서 다음과 같은 오류 메시지가 나타납니다.

The current request for action 'Login' on controller type 'MyController' is ambiguous between the following action methods: 
System.Web.Mvc.ActionResult Login() on type MyApp.Web.Controllers.MyController 
System.Web.Mvc.ActionResult Login(MyApp.Web.Models.LoginModel) on type MyApp.Web.Controllers.MyController 

ASP.NET MVC 4에서 기본 양식을 어떻게 설정합니까? ASP.NET MVC 4에서 예제 인터넷 응용 프로그램 템플릿을 살펴 보았습니다. 그러나 라우팅이 어떻게 연결되는지 파악할 수 없습니다. 도와 주셔서 정말 감사합니다.

+0

맞춤 경로를 사용하여 달성하려는 목표는 무엇입니까? 유청은 기본 경로 ({controller}/{action}/{id})를 그대로 두지 않았습니까? 기본 경로가 정상적으로 작동해야합니다. –

+0

@KevinJunghans - 당신이 알아 차리지 못했을 경우, 그의 컨트롤러 이름은 그의 URL과 다릅니다. –

+1

'[ViewSettings (Minify = true)] 란 무엇입니까? 이 속성에 대한 참조를 찾을 수 없습니다. 그것은 당신이 창조 한 것입니까? –

답변

7

아직 시도하지는 않았지만 적절한 Http 동사로 로그인 작업에 주석을 추가 할 수 있습니까? 나는 로그인 페이지를보기 위해 GET, 로그인 처리를 위해 POST을 사용하고 있다고 가정합니다.

첫 번째 작업에 [HttpGet]을 추가하고 두 번째 작업에 [HttpPost]을 추가하면 이론적으로 ASP.Net의 라우팅에서 어떤 방법이 사용되었는지에 따라 호출 할 Action 메서드를 알 수 있습니다. 문제가 해결되지 않으면

[HttpGet] // for viewing the login page 
[ViewSettings(Minify = true)] 
public ActionResult Login() 
{ 
    return View("~/Views/Account/Login.cshtml"); 
} 

[HttpPost] // For processing the login 
[ViewSettings(Minify = true)] 
[AllowAnonymous] 
[ValidateAntiForgeryToken] 
public ActionResult Login(LoginModel model) 
{ 
    return View("~/Views/Account/Login.cshtml"); 
} 

는 두 개의 경로 아래 같은 두 개의 다른 이름 작업을 고려해 : 코드는 다음과 같이 표시해야 다른 유사한 질문과 답변에 있습니다

routes.MapRoute(
    "DesktopLogin", 
    "{controller}/account/login", 
    new { controller = "My", action = "Login" } 
); 

routes.MapRoute(
    "DesktopLogin", 
    "{controller}/account/login/do", 
    new { controller = "My", action = "ProcessLogin" } 
); 

StackOverflow 이미,보십시오 : How to route GET and DELETE for the same url 그리고도 도움이 될 ASP.Net documentation가 있습니다.

+0

'[HttpGet]은 선택 사항이지만 대부분의 경우에는 필요하지 않지만 첫 번째 방법은 일반적으로 받아 들여지는 방법입니다. –

+0

두 메소드 모두에서 [AllowAnonymous]'를 원할 수도 있습니다. –