如何在MVC 4中制作自定义路由 [英] how to make custom route in MVC 4

查看:78
本文介绍了如何在MVC 4中制作自定义路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,

是否有可能制作一些文件夹系统控制器和视图..?



我做了你说,但仍然没有工作..

再次我重复我的结构..

控制器

/物业

SearchPropertyController.cs --->我有一个方法是callesCreate()



现在我的View结构是这样的。

浏览次数

/ Property

/ SearchProperty

Create.cshtml





现在我的路线是这样的



Dear All,
Does it is possible to make some folder system controller and view..?
Hi,
I did as you said, but still not working..
again I am repeating my structure..
Controllers
/Property
SearchPropertyController.cs ---> inside I have a method that is calles "Create()"

now my View structure is like this.
Views
/Property
/SearchProperty
Create.cshtml


now my routes like this

routes.MapRoute(
                    name: "Search_Mapping",
                    url: "Property/SearchProperty/{controller}/{action}/{id}",
                    defaults: new { controller = "SearchProperty", action = "Create", id = UrlParameter.Optional }
                    );





和现在我打电话给这个





and now I am calling like this

@Html.ActionLink("Search", "Create", "SearchProperty")







我无法获得任何解决方案..?



谢谢你,







谢谢,

-Jitendra Jadav。




I am not able to get any resolution..?

thank you,



Thank you,
-Jitendra Jadav.

推荐答案

在Controllers文件夹中创建一个TestController

在Views中创建一个Test文件夹在其中创建一个视图

在RouteConfig.cs中配置自定义路由,如下所示



Create a TestController in the Controllers folder
Create a Test folder in Views and Create a view in it
Have the custom route configured in RouteConfig.cs as below

routes.MapRoute(
                name: "TestRoute",
                url: "Folder1/Folder2/Test/{action}/{id}",
                defaults: new { controller = "Test", action = "GetAllSearchResult", id = UrlParameter.Optional }
            );







所以当你尝试以下网址(路线)



{root} / Folder1 / Folder2 / Test





GetAllSearchResult 将触发操作并呈现相应的视图



您不必在文件夹中创建文件夹控制器有自定义路线。可以在RouteConfig.cs中配置自定义路由。



希望它有所帮助!




So when you try the following url (route)

{root}/Folder1/Folder2/Test


GetAllSearchResult action will be triggered and corresponding view will be rendered

You donot have to create folder within folder in the controllers to have a custom route. A custom route can be configured in the RouteConfig.cs.

hope it helps!


备用,您可以考虑查看在MVC 4中创建和注册区域。

MVC区域示例 [ ^ ]
Alternate, You can consider looking into creating and registering Areas in MVC 4.
MVC Areas with example[^]


上述情况下您的路由配置错误。它应该是

Your route configuration is wrong for the above case. It should be
routes.MapRoute(
               name: "Search_Mapping",
               url: "Property/{controller}/{action}/{id}",
               defaults: new { controller = "SearchProperty", action = "Create", id = UrlParameter.Optional });


这篇关于如何在MVC 4中制作自定义路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆