我在调试我认为是 nopCommerce 2.3 上的路由问题时遇到问题。
站点地图和搜索页面都重定向到主页而不是各自的页面。这是 RouteProvider.cs 中的代码:
//product search
routes.MapLocalizedRoute("ProductSearch", "search/", new { controller = "Catalog", action = "Search" }, new[] { "Nop.Web.Controllers" });
这是 CatalogController.cs 代码:
public ActionResult Search(SearchModel model, SearchPagingFilteringModel command)
{
if (model == null)
model = new SearchModel();
if (command.PageSize <= 0) command.PageSize = _catalogSettings.SearchPageProductsPerPage;
if (command.PageNumber <= 0) command.PageNumber = 1;
if (model.Q == null)
model.Q = "";
model.Q = model.Q.Trim();
var categories = _categoryService.GetAllCategories();
if (categories.Count > 0)
{
model.AvailableCategories.Add(new SelectListItem()
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
foreach(var c in categories)
model.AvailableCategories.Add(new SelectListItem()
{
Value = c.Id.ToString(),
Text = c.GetCategoryBreadCrumb(_categoryService),
Selected = model.Cid == c.Id
});
}
var manufacturers = _manufacturerService.GetAllManufacturers();
if (manufacturers.Count > 0)
{
model.AvailableManufacturers.Add(new SelectListItem()
{
Value = "0",
Text = _localizationService.GetResource("Common.All")
});
foreach (var m in manufacturers)
model.AvailableManufacturers.Add(new SelectListItem()
{
Value = m.Id.ToString(),
Text = m.Name,
Selected = model.Mid == m.Id
});
}
IPagedList<Product> products = new PagedList<Product>(new List<Product>(), 0, 1);
// only search if query string search keyword is set (used to avoid searching or displaying search term min length error message on /search page load)
if (Request.Params["Q"] != null)
{
if (model.Q.Length < _catalogSettings.ProductSearchTermMinimumLength)
{
model.Warning = string.Format(_localizationService.GetResource("Search.SearchTermMinimumLengthIsNCharacters"), _catalogSettings.ProductSearchTermMinimumLength);
}
else
{
int categoryId = 0;
int manufacturerId = 0;
decimal? minPriceConverted = null;
decimal? maxPriceConverted = null;
bool searchInDescriptions = false;
if (model.As)
{
//advanced search
categoryId = model.Cid;
manufacturerId = model.Mid;
//min price
if (!string.IsNullOrEmpty(model.Pf))
{
decimal minPrice = decimal.Zero;
if (decimal.TryParse(model.Pf, out minPrice))
minPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(minPrice, _workContext.WorkingCurrency);
}
//max price
if (!string.IsNullOrEmpty(model.Pt))
{
decimal maxPrice = decimal.Zero;
if (decimal.TryParse(model.Pt, out maxPrice))
maxPriceConverted = _currencyService.ConvertToPrimaryStoreCurrency(maxPrice, _workContext.WorkingCurrency);
}
searchInDescriptions = model.Sid;
}
//products
// RedMorello 170212 - Change from .Position to .Price
products = _productService.SearchProducts(categoryId, manufacturerId, null,
minPriceConverted, maxPriceConverted, 0,
model.Q, searchInDescriptions, _workContext.WorkingLanguage.Id, null,
ProductSortingEnum.Price, command.PageNumber - 1, command.PageSize);
model.Products = products.Select(x => PrepareProductOverviewModel(x)).ToList();
model.NoResults = !model.Products.Any();
}
}
model.PagingFilteringContext.LoadPagedList(products);
return View(model);
}
[ChildActionOnly]
public ActionResult SearchBox()
{
return PartialView();
}