【问题标题】:MVC 5 Routing AttributeMVC 5 路由属性
【发布时间】:2016-11-01 07:23:00
【问题描述】:

我有 Home Controller,我的 Action 名称是 Index。在我的路线配置中,路线如下所示。

routes.MapRoute(
    "Default",   // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }  // Parameter defaults
);

现在我将我的页面称为http://localhost:11045/Home/Index 是正确的。

如果我像下面这样调用我的页面,它应该重定向到错误页面。
localhost:11045/Home/Index/98
localhost:11045/Home/Index/?id=98

如何使用路由属性来处理这个问题。

我在控制器中的操作如下所示。

public ActionResult Index() 
{
  return View(); 
}

【问题讨论】:

  • 请编辑并澄清您的问题。 “对”和“错”是什么意思?您需要哪些 URL 才能工作,哪些 URL 需要阻止?您是否在“错误”的情况下遇到错误?如果是这样,它是什么?此外,根据您的标题,不清楚问题是关于属性路由还是基于约定的路由(基于约定的路由通常是您的默认路由)。
  • 如果您满意,请您标记为已回答好吗?如果您还有任何问题,请在此处添加您的 cmets

标签: c# asp.net asp.net-mvc asp.net-mvc-routing


【解决方案1】:

对于Attribute Routing in ASP.NET MVC 5

像这样装饰你的控制器

[RoutePrefix("Home")]
public HomeController : Controller {
    //GET Home/Index
    [HttpGet]
    [Route("Index")]
    public ActionResult Index() {
        return View(); 
    }
}

并像这样在路由表中启用它

public class RouteConfig {

    public static void RegisterRoutes(RouteCollection routes) {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        //enable attribute routing
        routes.MapMvcAttributeRoutes();

        //convention-based routes
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = "" }
        );
    }
}

【讨论】:

  • routes.MapMvcAttributeRoutes(); 伙计,你救了我的命!!谢谢。
  • 对于混合项目 - Web API 和 MVC,有两个不同的功能需要启用。 WEB API - config.MapHttpAttributeRoutes(); MVC - routes.MapMvcAttributeRoutes();我们没有这个用于 MVC 的 :)
【解决方案2】:

请在此处查看有关路由的信息:http://www.asp.net/mvc/overview/older-versions-1/controllers-and-routing/asp-net-mvc-routing-overview-cs

很可能,默认路由应该如下所示:

routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

另外,索引操作方法似乎缺少参数,见下文:

public ActionResult Index(string id)
        {
            return View();
        }

尝试将string id 放入您的 Index 方法中。

【讨论】:

    【解决方案3】:
    public class URLRedirectAttribute : ActionFilterAttribute
        {
            public override void OnActionExecuted(ActionExecutedContext filterContext)
            {
                    string destinationUrl = "/VoicemailSettings/VoicemailSettings";
                    filterContext.Result = new JavaScriptResult()
                    {
                        Script = "window.location = '" + destinationUrl + "';"
                    };
            }
        }
    

    【讨论】:

      【解决方案4】:

      尝试将索引操作更改为:

      public ActionResult Index(int? id = null) 
      {
        return View(); 
      }
      

      这应该可以解决问题。因此,您可以使用“/{value}”将 id 作为参数传递,或者只使用“/?id={value}”

      【讨论】:

        猜你喜欢
        • 2013-10-28
        • 1970-01-01
        • 1970-01-01
        • 2015-12-18
        • 2014-04-14
        • 1970-01-01
        • 2014-02-12
        • 2014-11-14
        • 1970-01-01
        相关资源
        最近更新 更多