设置“主页” 在 Asp.Net MVC 中
在 asp.net MVC 中,“主页”(即点击 www.foo.com 时显示的路线)设置为 Home/Index 。
- 这个值存储在哪里?
- 如何更改“主页”?
- 还有什么比在家庭控制器的 Index 操作中使用 RedirectToRoute() 更优雅的吗?
我尝试在项目中 grep 查找 Home/Index,但找不到参考,也无法在 IIS (6) 中看到任何内容。 我查看了根目录中的 default.aspx 页面,但这似乎没有做任何相关的事情。
谢谢
In asp.net MVC the "homepage" (ie the route that displays when hitting www.foo.com) is set to Home/Index .
- Where is this value stored?
- How can I change the "homepage"?
- Is there anything more elegant than using RedirectToRoute() in the Index action of the home controller?
I tried grepping for Home/Index in my project and couldn't find a reference, nor could I see anything in IIS (6). I looked at the default.aspx page in the root, but that didn't seem to do anything relevent.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
查看
Default.aspx/Default.aspx.cs
和 Global.asax.cs您可以设置默认路由:
只需将控制器/操作名称更改为您想要的默认值即可。 这应该是路由表中的最后一条路由。
Look at the
Default.aspx/Default.aspx.cs
and the Global.asax.csYou can set up a default route:
Just change the Controller/Action names to your desired default. That should be the last route in the Routing Table.
ASP.NET Core
路由是在
Startup
类的Configure
方法中配置的。 要设置“主页”,只需添加以下内容。 当/如果用户导航到您网站的基本 URL 时,这将导致用户被路由到 MapRoute 方法中定义的控制器和操作,即 yoursite.com 会将用户路由到yoursite.com/foo/index:Pre-ASP.NET核心
使用位于 App_Start/RouteConfig.cs(MVC 3 和 4)或 Global.asax.cs(MVC 1 和 2)中的 RegisterRoutes 方法,如下所示。 如果用户导航到您网站的基本 URL,这将导致用户被路由到 MapRoute 方法中定义的控制器和操作,即 yoursite.com 会将用户路由到 yoursite.com/foo/index:
ASP.NET Core
Routing is configured in the
Configure
method of theStartup
class. To set the "homepage" simply add the following. This will cause users to be routed to the controller and action defined in the MapRoute method when/if they navigate to your site’s base URL, i.e., yoursite.com will route users to yoursite.com/foo/index:Pre-ASP.NET Core
Use the RegisterRoutes method located in either App_Start/RouteConfig.cs (MVC 3 and 4) or Global.asax.cs (MVC 1 and 2) as shown below. This will cause users to be routed to the controller and action defined in the MapRoute method if they navigate to your site’s base URL, i.e., yoursite.com will route the user to yoursite.com/foo/index:
第 1 步:单击解决方案中的 Global.asax 文件。
步骤 2:然后转到
RouteConfig.RegisterRoutes(RouteTable.Routes);
的定义步骤 3:更改控制器名称和视图名称
Step 1: Click on Global.asax File in your Solution.
Step 2: Then Go to Definition of
RouteConfig.RegisterRoutes(RouteTable.Routes);
Step 3: Change Controller Name and View Name
MVC 5 中的属性路由 在
MVC 5 之前,您可以通过调用 RouteConfig.cs 文件中的
routes.MapRoute(...)
将 URL 映射到特定操作和控制器。 这是存储主页 URL 的位置 (Home/Index
)。 但是,如果您如下所示修改默认路由,请记住这将影响其他操作和控制器的 URL。 例如,如果您有一个名为
ExampleController
的控制器类,并且其中有一个名为DoSomething
的操作方法,则预期的默认 urlExampleController/DoSomething
将不再工作,因为默认路由已更改。解决此问题的方法是不要弄乱默认路由,并在 RouteConfig.cs 文件中为其他操作和控制器创建新路由,如下所示,
现在是
ExampleController
DoSomething 操作> 类将映射到 urlhey/now
。 但是,每次您想要为不同的操作定义路线时,这可能会变得很乏味。 因此,在 MVC 5 中,您现在可以添加属性来将 url 与操作相匹配,如下所示,Attribute Routing in MVC 5
Before MVC 5 you could map URLs to specific actions and controllers by calling
routes.MapRoute(...)
in the RouteConfig.cs file. This is where the url for the homepage is stored (Home/Index
). However if you modify the default route as shown below,keep in mind that this will affect the URLs of other actions and controllers. For example, if you had a controller class named
ExampleController
and an action method inside of it calledDoSomething
, then the expected default urlExampleController/DoSomething
will no longer work because the default route was changed.A workaround for this is to not mess with the default route and create new routes in the RouteConfig.cs file for other actions and controllers like so,
Now the
DoSomething
action of theExampleController
class will be mapped to the urlhey/now
. But this can get tedious to do for every time you want to define routes for different actions. So in MVC 5 you can now add attributes to match urls to actions like so,检查 global.asax.cs 中的 RegisterRoutes 方法 - 这是路由配置的默认位置...
check RegisterRoutes method in global.asax.cs - it's the default place for route configuration...
我尝试了答案,但它对我不起作用。 这就是我最终要做的:
创建一个新的控制器 DefaultController。 在索引操作中,我编写了一行重定向:
在 RouteConfig.cs 中,更改路由的controller=“Default”。
I tried the answer but it didn't worked for me. This is what i ended up doing:
Create a new controller DefaultController. In index action, i wrote one line redirect:
In RouteConfig.cs, change controller="Default" for the route.
如果你不想改变路由器,只需进入HomeController
并在索引中更改 MyNewViewHere,如下所示:
If you don't want to change the router, just go to the HomeController
and change MyNewViewHere in the index like this: