创建自定义URL以在MVC中进行自定义搜索

时间:2014-05-03 22:21:24

标签: c# asp.net-mvc asp.net-mvc-routing

如何创建URL,例如www.company.com/clientNameA,www.company.com / clientNameB,以便我可以使用clientNameA或clientNameB并在MVC中调用操作?

1 个答案:

答案 0 :(得分:1)

在路由配置中,尝试以下内容:

如前所述,OP正在寻找以下路由表:

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

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

替代单独的控制器/操作:

路线表

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

routes.MapRoute(
          name: "Foo",
          url: "{client}",
          defaults: new { 
             controller = "Foo", 
             action = "YourAction", 
             client = UrlParameter.Optional 
          });

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

并添加一个控制器:

控制器

public class FooController : Controller
{
    //
    // GET: /Foo/
    public ActionResult YourAction(string client)
    {
        return null;
    }
}