我正在构建一个C#应用程序,一切似乎都是正确的,但我遇到了以下错误:
' /'中的服务器错误应用
无法找到资源。
描述:HTTP 404.您正在查找的资源(或其中一个依赖项)可能已被删除,名称已更改或暂时不可用。请查看以下网址,确保拼写正确。
请求的网址:/ Products / Main
一切似乎都是正确的。我有产品型号:
public class ProductsController : Controller
{
// GET: Product
public ActionResult Main(string category,
string name,
SortCriteria? criteria,
SortOrder? order)
{
var model = new ProductsViewModel
{
Category = category,
Name = name,
Criteria = criteria ?? SortCriteria.Name,
Order = order ?? SortOrder.ASC
};
var products = new ProductRepository().GetAll();
model.Categories =
products.Select(p => p.Category).Distinct().Select(c => new SelectListItem { Value = c, Text = c }).ToList();
if (!string.IsNullOrEmpty(category))
{
products = products.Where(p => p.Category.Equals(category));
}
if (!string.IsNullOrEmpty(name))
{
products = products.Where(p => p.Name.ToLower().Contains(name.ToLower()));
}
if (criteria == SortCriteria.Name)
{
if (order == SortOrder.DESC)
products = products.OrderByDescending(p => p.Name);
else
products = products.OrderBy(p => p.Name);
}
else
{
if (order == SortOrder.DESC)
products = products.OrderByDescending(p => p.Price);
else
products = products.OrderBy(p => p.Price);
}
model.Products = products.Select(MapToModel).ToList();
return View(model);
}
[HttpGet]
public ActionResult Create()
{
return View(new ProductViewModel());
}
[HttpPost]
public ActionResult Create(ProductViewModel model)
{
new ProductRepository().Create(MapFromModel(model));
return RedirectToAction("Index");
}
private ProductViewModel MapToModel(Product p)
{
return new ProductViewModel
{
Id = p.ID,
Name = p.Name,
Price = p.Price,
Category = p.Category
};
}
private Product MapFromModel(ProductViewModel p)
{
return new Product
{
ID = p.Id,
Name = p.Name,
Price = p.Price
};
}
}
我有一个关于此主要操作结果的视图。
这是我的网址格式:
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new {culture="en", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
答案 0 :(得分:-2)
输入控制器名称和操作方法名称,否则在route.config文件中更改控制器和操作方法名称...
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new {culture="en", controller = "Products", action = "Main", id = UrlParameter.Optional }
);