2016-02-29 63 views
0

我使用MVC 4文件夹名称,并需要从地址栏中删除/首页/文件夹...MVC 4 - 删除/首页/从地址栏并保留其他

http://localhost:61700/Home/AboutUs

需要更改为...

http://localhost:61700/AboutUs

我做到了增槎在nging默认的控制器 “RouteConfig.cs

public class RouteConfig 
{ 
     public static void RegisterRoutes(RouteCollection routes) 
     { 
       routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

       routes.MapRoute(
         name: "Default", 
         //url: "{controller}/{action}/{id}", 
         url: "{action}/{id}", 
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
       ); 
     } 
} 

上面的代码按预期方式工作。我有另一个文件夹, 品牌,管理等...在这里我想说明的网址为 http://localhost:61700/brand/productInfo ...但我在这里获取服务器 误差Server Error in '/' Application.

有人可以给我建议,我在哪里我做错了?

截图此处获得更多信息:

enter image description here

+2

你需要添加一个具有'url:“AboutUs,''和'defaults:{controller =”Home“,action =”AboutUs“'(并且放在默认路由之前)的默认路由 –

+0

Hi ** @ StephenMuecke **感谢您的回答,但我在主文件夹中有160多个视图,我是否需要提及所有** ulrs **? – Reddy

+1

如果你想剥离'/ Home /',那么是的,你将需要一个具体的路线 –

回答

2

这是当前的RouteConfig.cs配置:

routes.MapRoute(
    name: "Default", 
    url: "{action}/{id}", 
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
); 

你告诉Asp.net,当一个请求到达时,假设第一个参数为action,并作为id第二个参数。现在你不告诉Asp.net解析任何参数作为controller。因此,它使用默认值(作为MapRoute方法的第三个参数给出),在此情况下为Home

在这种情况下解析请求http://localhost:61700/AboutUs时的值最终被:

  • 控制器:Home(它使用默认控制器)
  • 动作:AboutUs(从第一个参数)
  • ID :null(这个不重要)

当解析请求时http://localhost:61700/brand/productInfo值最终被:

  • 控制器:Home(它使用默认的控制器,因为你没有指定从哪里获取控制器名称)
  • 行动:Brand(从第一个参数)
  • ID:"productInfo"

你得到的错误是因为没有在HomeController.cs名为类型的参数Brand操作方法210。

Asp.net通过尝试匹配配置的路由来处理传入请求,并使用匹配的第一个路由。

有几种方法来实现你想要的,包括但不限于:

  1. 手工绘制的每一个动作在你HomeController.cs(选择此方法将取决于行动的数量在HomeController )。这看起来像:

    routes.MapRoute(
        name: "AboutUs", 
        url: "AboutUs", 
        defaults: new { controller = "Home", action = "AboutUs" } 
        ); 
    
    routes.MapRoute(
        name: "ContactUs", 
        url: "ContactUs", 
        defaults: new {controller = "Home", action = "ContactUs" } 
        ); 
    
    // etc... 
    
    routes.MapRoute(
        name: "Default", 
        url: "{controller}/{action}/{id}", 
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        ); 
    

    注意默认路由如何是最后一个,这一点很重要,因为它比别人少具体,之前如果把将匹配的要求,并希望寻找一个AboutUsController

  2. 您可以使用路由约束。这看起来像:

    route.MapRoute(
        name: "HomeControllerRoutes", 
        url: "{action}/{id}", 
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
        constraints: new { action = "AboutUs|ContactUs|etc..." } //Here you would put all your action methods from home controller that you want to accces as /{action} 
    ); 
    
    routes.MapRoute(
        name: "Default", 
        url: "{controller}/{action}/{id}", 
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
        ); 
    

    如果您想了解更多关于路线的限制,我发现this文章,解释了constrains参数可以接收正则表达式(我建议你修改上述正则表达式,使它不区分大小写)IRouteConstraint

更新:

我刚刚看了你的关于在你的HomeController 160+行动,将让你的正则表达式在我的第二个建议相当长的评论。在这种情况下,您有其他选项可以是:

  • 使用正则表达式,拒绝所有其他控制器的名字,但是这将违反开/闭原则(OCP),每当你添加另一个控制器时,你必须将它添加到正则表达式中。

  • 从您的元数据HomeController类创建正则表达式。这看起来像

    string.Join("|", typeof(HomeController).GetMethods().Select(info => info.Name)) 
    
  • 或者你可以看看IRouteConstraint看到,如果你能想出一个更好的解决方案。

    我有IRouteConstraint

  • +0

    嗨** @ mamodom ** ...感谢您的更新。我会和你的**选项2 **一起去,尽管我有很多文件,但并不复杂。 **我是upvoting并接受你的答案**。谢谢你somuch详细解释你的时间... – Reddy

    +1

    这就是为什么我们在这里! –

    1

    在route.config/glibal.asax添加这和不改变你的缺省路由。在上面添加以下内容。

    routes.MapRoute(
         name: "About",  
         url: "AboutUs", 
         defaults: new { controller = "Home", action = "AboutUs" } 
    ); 
    
    routes.MapRoute(
         name: "Default", 
         url: "{controller}/{action}/{id}", 
         defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
    
    +0

    ** @ Suprabhat Biswal **我应该需要添加上面的代码160+次,因为我在家里有很多文件控制器? – Reddy

    +0

    @Reddy什么样的文件?你可以说得更详细点吗? – Prabhat

    +0

    对于诸如“* http:// localhost:61700/AboutUs *”或“* http:// localhost:61700 /”之类的特殊情况,默认路径将处理正常流程“* http:// localhost:61700/brand/productInfo *联系人*“等你需要写不同的路线。 – Prabhat

    0

    没有经验。我在家里控制器160+意见

    你不提你有多少的观点在其他控制器,也没有他们需要成为多么复杂。

    与其保持默认的controller/action并为家中每个视图添加路由,您可以为每个控制器添加一个路由,然后使用没有控制器路径的默认路由。

    虽然这意味着您确实需要每个控制器的路径,但它比每个视图都要好。

    routes.MapRoute(
        "AdminRoute", 
        "Admin/{action}/{id}", 
        new { controller = "Admin", action = "Index", id = UrlParameter.Optional }); 
    
    routes.MapRoute(
        "BrandRoute", 
        "Brand/{action}/{id}", 
        new { controller = "Brand", action = "Index", id = UrlParameter.Optional }); 
    
    routes.MapRoute(
        "HomeRoute", 
        "{action}/{id}", 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 
    
    routes.MapRoute(
        "DefaultRoute", 
        "{controller}/{action}/{id}", 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional }); 
    

    (AFAICR你并不需要,因为所有的意见会被其他3条路线覆盖默认路由)

    注意事项“HomeRoute”的道路没有一个控制器。

    只要他们是以此顺序带/ Admin /或/ Brand /的任何网址将首先被接收。

    相关问题