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

本文关键字:自定义 搜索 MVC URL 以在 创建 | 更新日期: 2023-09-27 18:01:26

我如何创建URL的www.company.com/clientNameA, www.company.com/clientNameB这样我就可以采取clientNameA或clientNameB和调用MVC中的动作?

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

在你的路由配置中,尝试这样做:

如前所述,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;
    }
}