拖曳方式在具有路由属性的 asp.net mvc 中调用操作 - 路由 asp.net mvc



All我想要 2 种调用操作的方式。例如

http://localhost:16800/Content1/1/text

 http://localhost:16800/Content1/1

我的路由配置是默认路由。 我使用 from 路由属性 .

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapMvcAttributeRoutes();
        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );

我的控制器使用了路由属性,因此:

namespace WebApplication2.Controllers

{

 [RoutePrefix("Content1")]
[Route("{action=Index}")]
public class Content1Controller : Controller
{
    [Route("{id}/{text}")]
    public ActionResult Index(int id, string text)
    {
        return View();
    }
}

}现在,这种方式为我工作。 http://localhost:16800/Content1/1/text

这种方式对我不起作用。 http://localhost:16800/Content1/1 我只是使用两种方式来调用我的行动。提醒我在控制器上使用[Route("{action=Index}")],我不需要在 url 中定义操作名称。

Try this, It will work(tested).

namespace WebApplicationExamples.Controllers
    {
        [RoutePrefix("Content1")]
        [Route("{action=Index}")]
        public class Content1Controller : Controller
        {
            // GET: Content1
            [Route("{id}")]
            [Route("{id}/{text}")]
            public ActionResult Index()
            {
                return View();
            }
        }
    }

最新更新