简体   繁体   English

MVC路由问题如何设置所有Controller的默认动作?

[英]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.

相关问题 在asp.net mvc中设置索引操作默认操作控制器 - set index action default action controller in asp.net mvc 在ASP.NET MVC 3中为控制器设置默认操作(而不是索引) - Set default action (instead of index) for controller in ASP.NET MVC 3 如何让ASP.NET MVC为区域设置默认的Controller和Action? - How do I get ASP.NET MVC to set a default Controller and Action for an Area? 如果我在 mvc5 中将默认路由和属性路由设置为相同的 controller 则常规路由不起作用 - If I set default route and attribute routing to same controller in mvc5 then conventional routing is not working MVC路由不适用于虚拟目录上的非默认/根操作控制器 - MVC routing does not work for non-default/root action controller on virtual directory 将根请求路由到此控制器并在MVC中执行操作 - Routing root request to this controller and action in MVC 如何在MVC属性路由中设置控制器名称? - How to set the controller's name in MVC attribute routing? Web API路由{controller} / {action} / {id}与默认路由 - Web API Routing {controller}/{action}/{id} vs default routing 默认控制器操作在 .NET 6 MVC 中如何工作? - How does the default controller action work in .NET 6 MVC? 如何在 asp.net MVC 4 &amp; MVC 5 中设置默认控制器 - How to set Default Controller in asp.net MVC 4 & MVC 5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM