[英]Problem with MVC Routing how to set default action of all Controller?
my RouteConfig like this : 我的RouteConfig是这样的:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new[] { "GPTKish.Controllers" }
);
When I enter for ex : http://localhost:23594/News , show me Index Action of News Controller but when i enter http://localhost:23594/NewsImages , get HTTP Error 403.14 - Forbidden!!!! 当我输入ex: http:// localhost:23594 / News时 ,显示新闻控制器的索引操作但是当我输入http:// localhost:23594 / NewsImages时 ,得到HTTP错误403.14 - 禁止!!!! and don't show index action of NewsImages Controller !!!
并且不显示NewsImages Controller的索引动作! where is wrong of my code?
我的代码错在哪里? this is my newsImages controller
这是我的newsImages控制器
public class NewsImagesController : Controller
{
private DatabaseContext db = new DatabaseContext();
// GET: NewsImages
public ActionResult Index(int selectedNewsid)
{
List<NewsImage> newsImages = db.NewsImages.Include(n => n.News).Where(c => c.NewsId == selectedNewsid).ToList();
ViewBag.NewsTitle = newsImages[1].News.Title;
return View(newsImages);
}
thank you 谢谢
It's because Index is expecting a parameter: selectedNewsid. 这是因为Index期待一个参数:selectedNewsid。
http://localhost:23594/NewsImages?selectedNewsid=0 or (if using the HttpGet Attribute) http://localhost:23594/NewsImages/0 should resolve. http:// localhost:23594 / NewsImages?selectedNewsid = 0或(如果使用HttpGet属性) http:// localhost:23594 / NewsImages / 0应解决。
Two options: 两种选择:
1) Make selectedNewsid be optional and (optional) add a HttpGet Attribute (due to the parameter) 1)使selectedNewsid成为可选项并且(可选)添加HttpGet属性(由于参数)
[HttpGet("{selectedNewsid")]
public ActionResult Index(int selectedNewsid = 0)
{
if(selectedNewsid == 0)
{
//Show all news images
}else{
List<NewsImage> newsImages = db.NewsImages.Include(n => n.News).Where(c => c.NewsId == selectedNewsid).ToList();
ViewBag.NewsTitle = newsImages[1].News.Title;
return View(newsImages);
}
}
2) Create a new default action without a parameter 2)创建没有参数的新默认操作
public ActionResult Index()
{
return View();
}
This URL - http://localhost:23594/NewsImages - doesn't provide a value for selectedNewsId
. 此URL - http:// localhost:23594 / NewsImages - 未提供
selectedNewsId
的值。 If you want to show images for NewsId = 5, the URL should be http://localhost:23594/NewsImages?selectedNewsId=5 如果要显示NewsId = 5的图像,则URL应为http:// localhost:23594 / NewsImages?selectedNewsId = 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.