默认属性路由在ASP.NET MVC中不起作用。

huangapple go评论65阅读模式
英文:

Default attribute routing not worrking in ASP.NET MVC

问题

I'm trying to use attribute routing in my application. I provided the route attributes to all action methods and also provided the HTTP verbs to the action methods. But I am not able to set the default routing. If I manually provided the route in the URL, it's working but not able to see by default on application running. I used the empty [route("")] attribute for default.

Controller code:

namespace WebApplication2.Controllers
{
    [RoutePrefix("myproject")]
    public class AttributerouteController : Controller
    {
        // GET: Attributeroute
        [HttpGet]
        [Route("")]
        public ActionResult Employees()
        {
            return View();
        }

        [HttpGet]
        [Route("Employees/{id}")]
        public ActionResult Employeebyid(int id)
        {
            return View();
        }

        [HttpGet]
        [Route("Employees/{id}/department")]
        public ActionResult Empdepartment(int id)
        {
            return View();
        }
    }
}

My routeconfig.cs code:

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

    routes.MapMvcAttributeRoutes();
    //routes.RouteExistingFiles = true;
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Attributeroute", action = "Employees", id = UrlParameter.Optional }
    );
}
英文:

I'm trying to use attribute routing in my application. I provided the route attributes to all action methods and also provided the HTTP verbs to the action methods. But I am not able to set the default routing. If I manually provided the route in the URL, it's working but not able to see by default on application running. I used the empty [route("")] attribute for default.

Controller code :

namespace WebApplication2.Controllers
{
    [RoutePrefix("myproject")]
    public class AttributerouteController : Controller
    {
        // GET: Attributeroute
        [HttpGet]
        [Route("")]
        public ActionResult Employees()
        {
            return View();
        }

        [HttpGet]
        [Route("Employees/{id}")]
        public ActionResult Employeebyid(int id)
        {
            return View();
        }

        [HttpGet]
        [Route("Employees/{id}/department")]
        public ActionResult Empdepartment(int id)
        {
            return View();
        }
    }
}

My routeconfig.cs code :

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

    routes.MapMvcAttributeRoutes();
    //routes.RouteExistingFiles = true;
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Attributeroute", action = "Employees", id = UrlParameter.Optional }
    );
}

答案1

得分: 0

在需要的情况下,在方法属性上使用波浪号 ~ 来覆盖路由前缀:

[RoutePrefix("myproject")]
public class AttributerouteController : Controller
{         
    [Route("~/")] 
    public ActionResult Employees()
    {
        return View();
    }                    

    [Route("Employees/{id}")] //http://localhost:12345/myproject/Employees/66           
    public ActionResult Employeebyid(int id)
    {
        //return View(nameof(Employees));
    }
  
    [Route("Employees/{id}/department")] // http://localhost:12345/myproject/Employees/66777/department     
    public ActionResult Empdepartment(int id)
    {
        //return View(nameof(Employees));
    }
}

顺便提一下,在操作方法上可以省略 HttpGet 属性。它是默认的 HTTP 动词。

<hr>

您还可以在 Microsoft 博客上找到其他有用的信息,链接如下:ASP.NET MVC 5 中的属性路由

英文:

Use a tilde ~ on the method attribute to override the route prefix if needed:

[RoutePrefix(&quot;myproject&quot;)]  
public class AttributerouteController : Controller
{         
    [Route(&quot;~/&quot;)] 
    public ActionResult Employees()
    {
        return View();
    }                    

    [Route(&quot;Employees/{id}&quot;)] //http://localhost:12345/myproject/Employees/66           
    public ActionResult Employeebyid(int id)
    {
        //return View(nameof(Employees));
    }
  
    [Route(&quot;Employees/{id}/department&quot;)] // http://localhost:12345/myproject/Employees/66777/department     
    public ActionResult Empdepartment(int id)
    {
        //return View(nameof(Employees));
    }
}

By the way, you can omit the HttpGet attribute on the action methods. It is the default HTTP verb.

<hr>

An additional useful information you can find on the Microsoft blog, here: Attribute Routing in ASP.NET MVC 5

huangapple
  • 本文由 发表于 2023年6月9日 14:16:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76437676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定