如何在 ASP.NET MVC 的 HomeController 中定义自定义路由?
How can I define custom route in HomeController of ASP.NET MVC?
我在 ASP.NET MVC 的 HomeController 中定义自定义路由时遇到问题。
当我提交表单时,路由被定义为Home/BlogSearch/?searchKeyword=Lorem
。但是,我想得到这个 URL /blogs/blogsearch?searchKeyword=text
.
这是我的表格 html 部分,如下所示。
<form action="/Home/BlogSearch/">
<input type="text" name="searchKeyword" id="searchKeyword">
<button type="submit" id="searchButton"><i class="bi bi-search"></i></button>
</form>
这是我在 HomeController 中定义的 BlogSearch 函数。
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
{
var searchList = db.Blog.Include("Kategori").Where(
x => x.Icerik.Contains(searchKeyword)
).OrderByDescending(x => x.BlogId).ToPagedList(Sayfa, 5);
return View(searchList);
}
我写了这个路由,但是没有成功。
routes.MapRoute(
name: "BlogSearch",
url: "blogs/blogsearch",
defaults: new { controller = "Home", action = "BlogSearch" }
);
我该如何解决?
您可以在 App_Start 文件夹下的 RouteConfig.cs 中的 RouteConfig class 中注册路由。可以使用 MapRoute 扩展方法配置自定义路由。您需要在 MapRoute 函数中提供至少两个参数,即路由名称和 URL 模式。默认参数是可选的。
你可以参考这个link:Custom Routing in ASP.net MVC
尝试属性路由:
[Route("~/blogs/blogsearch/{searchKeyword}/{sayfa?}")]
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
你将不得不使用这个url
url /blogs/blogsearch/searchText
如果你还想使用这个url
url /blogs/blogsearch?searchKeyword=searchText
你可以试试
[Route("~/blogs/blogsearch")]
public ActionResult BlogSearch([FromQuery] string searchKeyword)
如果您使用 routeconfig ,则必须添加
routes.MapMvcAttributeRoutes();
你的观点
<form action="BlogSearch" controller="Blogs">
下面是我的解决方案。
我更改了下面定义的 url 而不是 /Home/BlogSearch/
<form action="/blogs/blogsearch">
</form>
下面是我的 BlogSearch 方法,其中添加了自定义 url
[Route("blogs/blogsearch")]
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
{
var searchList = db.Blog.Include("Kategori").Where(
x => x.Icerik.Contains(searchKeyword)
).OrderByDescending(x => x.BlogId).ToPagedList(Sayfa, 5);
return View(searchList);
}
我在 ASP.NET MVC 的 HomeController 中定义自定义路由时遇到问题。
当我提交表单时,路由被定义为Home/BlogSearch/?searchKeyword=Lorem
。但是,我想得到这个 URL /blogs/blogsearch?searchKeyword=text
.
这是我的表格 html 部分,如下所示。
<form action="/Home/BlogSearch/">
<input type="text" name="searchKeyword" id="searchKeyword">
<button type="submit" id="searchButton"><i class="bi bi-search"></i></button>
</form>
这是我在 HomeController 中定义的 BlogSearch 函数。
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
{
var searchList = db.Blog.Include("Kategori").Where(
x => x.Icerik.Contains(searchKeyword)
).OrderByDescending(x => x.BlogId).ToPagedList(Sayfa, 5);
return View(searchList);
}
我写了这个路由,但是没有成功。
routes.MapRoute(
name: "BlogSearch",
url: "blogs/blogsearch",
defaults: new { controller = "Home", action = "BlogSearch" }
);
我该如何解决?
您可以在 App_Start 文件夹下的 RouteConfig.cs 中的 RouteConfig class 中注册路由。可以使用 MapRoute 扩展方法配置自定义路由。您需要在 MapRoute 函数中提供至少两个参数,即路由名称和 URL 模式。默认参数是可选的。
你可以参考这个link:Custom Routing in ASP.net MVC
尝试属性路由:
[Route("~/blogs/blogsearch/{searchKeyword}/{sayfa?}")]
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
你将不得不使用这个url
url /blogs/blogsearch/searchText
如果你还想使用这个url
url /blogs/blogsearch?searchKeyword=searchText
你可以试试
[Route("~/blogs/blogsearch")]
public ActionResult BlogSearch([FromQuery] string searchKeyword)
如果您使用 routeconfig ,则必须添加
routes.MapMvcAttributeRoutes();
你的观点
<form action="BlogSearch" controller="Blogs">
下面是我的解决方案。
我更改了下面定义的 url 而不是 /Home/BlogSearch/
<form action="/blogs/blogsearch">
</form>
下面是我的 BlogSearch 方法,其中添加了自定义 url
[Route("blogs/blogsearch")]
public ActionResult BlogSearch(string searchKeyword, int Sayfa = 1)
{
var searchList = db.Blog.Include("Kategori").Where(
x => x.Icerik.Contains(searchKeyword)
).OrderByDescending(x => x.BlogId).ToPagedList(Sayfa, 5);
return View(searchList);
}