如何将字符串传递给我的控制器操作
How to pass a string to my controller action
如何映射以下 url...
域。com/Products/product-name-here
我想将其映射到我的产品控制器上的 GetProduct 操作。
这是我 route.config
中的内容
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Product",
url: "product/{id}"
);
routes.MapRoute(
name: "EditProduct",
url:"Admin/Product/{action}",
defaults: new { controller = "Products", action = "Index"}
);
routes.MapRoute(
name:"ProductPages",
url:"Products/{id}",
defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional }
);
routes.MapRoute(
name:"OrderRoute",
url:"Orders/",
defaults: new { controller= "Order", action="Index"}
);
}
这是我要将路线映射到的操作。
[HttpGet]
public ActionResult GetProduct(string pageURL)
{
if ( string.IsNullOrWhiteSpace(pageURL))
return View("PageNotFound");
var product = db.Products.Where(x => x.PageURL == pageURL);
return View("GetProduct");
}
此代码实际上并未调用操作:
routes.MapRoute(
name:"ProductPages",
url:"Products/{id}",
defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional }
);
您想添加一个使用像这样的动作的路由(注意动作标签):
routes.MapRoute(
name:"ProductPages",
url:"Products/{action}/{id}",
defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional }
);
添加:
routes.MapRoute(
name:"ProductPages",
url:"Products/{pageURL}",
defaults: new {controller = "Products", action = "GetProduct" }
);
重要提示:您的默认路线应该是您 route.config 中的最后一条路线。
在你的代码中它是第一个。
编辑:应删除或编辑您的实际路线 "ProductPages" 以避免与我的建议发生冲突。
如何映射以下 url...
域。com/Products/product-name-here
我想将其映射到我的产品控制器上的 GetProduct 操作。
这是我 route.config
中的内容 public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "Product",
url: "product/{id}"
);
routes.MapRoute(
name: "EditProduct",
url:"Admin/Product/{action}",
defaults: new { controller = "Products", action = "Index"}
);
routes.MapRoute(
name:"ProductPages",
url:"Products/{id}",
defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional }
);
routes.MapRoute(
name:"OrderRoute",
url:"Orders/",
defaults: new { controller= "Order", action="Index"}
);
}
这是我要将路线映射到的操作。
[HttpGet]
public ActionResult GetProduct(string pageURL)
{
if ( string.IsNullOrWhiteSpace(pageURL))
return View("PageNotFound");
var product = db.Products.Where(x => x.PageURL == pageURL);
return View("GetProduct");
}
此代码实际上并未调用操作:
routes.MapRoute( name:"ProductPages", url:"Products/{id}", defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional } );
您想添加一个使用像这样的动作的路由(注意动作标签):
routes.MapRoute(
name:"ProductPages",
url:"Products/{action}/{id}",
defaults: new {controller = "Products", action = "GetProduct", id = UrlParameter.Optional }
);
添加:
routes.MapRoute(
name:"ProductPages",
url:"Products/{pageURL}",
defaults: new {controller = "Products", action = "GetProduct" }
);
重要提示:您的默认路线应该是您 route.config 中的最后一条路线。 在你的代码中它是第一个。
编辑:应删除或编辑您的实际路线 "ProductPages" 以避免与我的建议发生冲突。