Asp.Net MVC에서 "홈페이지"설정
asp.net MVC에서 "홈페이지"(예 : www.foo.com을 누를 때 표시되는 경로)는 Home / Index로 설정됩니다.
- 이 값은 어디에 저장합니까?
- "홈페이지"는 어떻게 변경합니까?
- 홈 컨트롤러의 Index 작업에서 RedirectToRoute ()를 사용하는 것보다 더 우아한 조직입니까?
내 프로젝트에서 Home / Index에 대해 grepping을 시도했지만 참조를 수 없었고 IIS에서 아무것도 볼 수 없습니다 (6). 루트에서 default.aspx 페이지를 제어하지만 관련 작업을 수행하지 않습니다.
감사합니다
상기 봐 Default.aspx/Default.aspx.cs
와 Global.asax.cs
기본 경로를 접근 할 수 있습니다.
routes.MapRoute(
"Default", // Route name
"", // URL with parameters
new { controller = "Home", action = "Index"} // Parameter defaults
);
컨트롤러 / 액션 이름을 원하는대로 변경하기 만하면됩니다. 이것이 라우팅 테이블의 마지막 경로 집합니다.
ASP.NET Core
라우팅은 클래스 의 Configure
방법으로 구성 Startup
됩니다. "홈페이지"를 설정 비용으로 다음을 추가하면됩니다. 이렇게하면 사용자가 사이트의 기본 URL로 엄선 할 때 MapRoute 메소드에 정의 된 작업과 컨트롤러로 라우팅됩니다. 즉, yoursite.com은 사용자를 yoursite.com/foo/index로 라우팅합니다.
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=FooController}/{action=Index}/{id?}");
});
ASP.NET Core 이전
바로 같이 App_Start / RouteConfig.cs (MVC 3 및 4) 또는 Global.asax.cs (MVC 1 및 2)에있는 RegisterRoutes 메서드를 사용합니다. 이렇게하면 사용자가 사이트의 기본 URL로 이동하는 경우 MapRoute 메서드에 정의 된 작업과 컨트롤러로 라우팅됩니다. 즉, yoursite.com은 사용자를 yoursite.com/foo/index로 라우팅합니다.
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Here I have created a custom "Default" route that will route users to the "YourAction" method within the "FooController" controller.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "FooController", action = "Index", id = UrlParameter.Optional }
);
}
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Your Controller", action = "Your Action", id = UrlParameter.Optional }
);
}
}
1 단계 : 솔루션에서 Global.asax 파일을 클릭합니다.
2 단계 : 그런 다음 정의로 이동
RouteConfig.RegisterRoutes(RouteTable.Routes);
3 단계 : 컨트롤러 이름 및보기 이름 변경
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home",
action = "Index",
id = UrlParameter.Optional }
);
}
}
global.asax.cs의 RegisterRoutes 메서드를 확인하십시오. 경로 구성의 기본 위치입니다.
MVC 5의 속성 라우팅
MVC 5 이전 routes.MapRoute(...)
에는 RouteConfig.cs 파일 을 호출 하여 URL을 특정 작업 및 컨트롤러에 매핑 할 수있었습니다 . 여기에 홈페이지의 URL이 저장됩니다 ( Home/Index
). 그러나 아래와 같이 기본 경로를 수정하면
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
이는 다른 작업 및 컨트롤러의 URL에 영향을 미칩니다. 예를 들어 컨트롤러 클래스라는 이름이 ExampleController
있고 그 안에 라는 작업 메서드가있는 경우 기본 경로가 변경 되었기 때문에 DoSomething
예상되는 기본 URL ExampleController/DoSomething
이 더 이상 작동하지 않습니다.
이에 대한 해결 방법은 기본 경로를 엉망으로 만들지 않고 다른 작업 및 컨트롤러에 대해 RouteConfig.cs 파일에 새 경로를 만드는 것입니다.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Example",
url: "hey/now",
defaults: new { controller = "Example", action = "DoSomething", id = UrlParameter.Optional }
);
이제 클래스 의 DoSomething
액션이 ExampleController
url에 매핑됩니다 hey/now
. 그러나 이것은 다른 작업에 대한 경로를 정의하고 싶을 때마다 지루할 수 있습니다. 따라서 MVC 5에서는 URL을 이와 같은 작업에 일치시키는 속성을 추가 할 수 있습니다.
public class HomeController : Controller
{
// url is now 'index/' instead of 'home/index'
[Route("index")]
public ActionResult Index()
{
return View();
}
// url is now 'create/new' instead of 'home/create'
[Route("create/new")]
public ActionResult Create()
{
return View();
}
}
나는 대답을 시도했지만 그것은 나를 위해 일하지 않았습니다. 이것이 내가 한 일입니다.
새 컨트롤러 DefaultController를 만듭니다. 인덱스 작업에서 한 줄 리디렉션을 작성했습니다.
return Redirect("~/Default.aspx")
RouteConfig.cs에서 경로에 대한 controller = "Default"를 변경합니다.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Default", action = "Index", id = UrlParameter.Optional }
);
라우터를 변경하지 않으려면 HomeController로 이동하여 다음과 같이 색인에서 MyNewViewHere를 변경하십시오.
public ActionResult Index()
{
return View("MyNewViewHere");
}
참고 URL : https://stackoverflow.com/questions/1142003/set-homepage-in-asp-net-mvc
'IT' 카테고리의 다른 글
arrayfun은 matlab의 명시 적 루프보다 훨씬 느릴 수 있습니다. (0) | 2020.08.18 |
---|---|
Scala의 "flatmap that s ***"관용적 표현은 어디에서 왔습니까? (0) | 2020.08.16 |
부모 패딩 무시 (0) | 2020.08.16 |
"기본"MIME 유형이 있습니까? (0) | 2020.08.16 |
Session.Abandon ()과 Session.Clear ()의 차이점은 무엇입니까? (0) | 2020.08.16 |