[닷넷] Url.Action사용해서 원하는 URL연결하기
cshtml에서 Url.Action으로 a태그 href를 변경하고 싶었다.
파일구조
- Areas
- Student
- Controller
- LearningController.cs
- View
- Index.cshtml
- Controller
- Professor
- Controller
- StudyController.cs
- View
- Index.cshtml
- Controller
- Student
내가 원하는 것은 아래처럼 2개의 a태그에 각각 다른 컨트롤러의 url이다.
1 | // url결과 |
기존코드
1 | <a href="@Url.Action("Index","Learning")" id="menuUrl"> |
url결과는 아래와 같다
1 | // url결과 |
Action(string, string)은 처음 URI를 잡고있기때문에 Professor의 Study가 아니라 Student에서 Study를 찾는다.
해결법 Action(String, String, Object)
routeValues를 사용해서 area를 지정해주었다.
1 | <a href="@Url.Action("Index", "Learning", new { area = "Student" })" id="menuUrl"> |
new 연산자로 area를 지정했기에 해당 area아래의 Url을 각각찾느다
1 | // url결과 |
참고
MS사의 docs는 예시도 많이 없고 UI자체도 가독성이 많이 떨어진다. 그래도 docs 질은 괜찮은 건지 블로그글이 많이 없다. 닷넷개발자들은 영어고수인게 틀림없다.
![[OS/WINDOW]배포후 서버재시작에 batch와 윈도우 스케줄러 활용하기](https://cdn.pixabay.com/photo/2012/03/04/00/50/board-22098_960_720.jpg)
![[블로그]헥소테마에서 댓글기능 facebook에서 utterances로 변경하기](https://miro.medium.com/max/1600/1*aOv6h3h_v9PQWa03zGACnw.png)