简体   繁体   中英

MVC routing my GET request to the wrong action

I've got a URL routing issue in my application. I've tried to tackle the issue, but haven't really made any headway.

So, I'm making the following AJAX request client-side:

$.ajax({
    type: 'GET',
    url: programState.getBaseUrl() + 'Playlist/GetPlaylistsByUserId',
    dataType: 'json',
    data: {
        userId: user.get('id')
    },
    success: function (data) {
        console.log("JSON data:", data);
    },
    error: function(error) {
        console.error(error);
    }
});

Here's the network:

在此输入图像描述

Here's the server error:

在此输入图像描述

Here's the Controller's methods for GET and GetPlaylistsByUserId:

[HttpGet]
public ActionResult Get(Guid id)
{
    Playlist playlist = PlaylistDao.GetById(id);

    return new JsonDataContractActionResult(playlist);
}

[HttpGet, ActionName("GetPlaylistsByUserId")]
public ActionResult GetPlaylistsByUserId(Guid userId)
{
    IList<Playlist> playlists = PlaylistDao.GetByUserId(userId);

    return new JsonDataContractActionResult(playlists);
}

and finally, here are my routes:

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

routes.MapRoute(
    "post-Playlist",
    "{controller}",
    new { controller = "Playlist", action = "create" },
    new { httpMethod = new HttpMethodConstraint("POST") }
    );

routes.MapRoute(
    "get-Playlist",
    "{controller}/{action}/{id}",
    new { controller = "Playlist", action = "get" },
    new { httpMethod = new HttpMethodConstraint("GET") }
    );

routes.MapRoute(
    "put-Playlist",
    "{controller}",
    new { controller = "Playlist", action = "update" },
    new { httpMethod = new HttpMethodConstraint("PUT") }
    );

routes.MapRoute(
    "delete-Playlist",
    "{controller}/{id}",
    new { controller = "Playlist", action = "delete" },
    new { httpMethod = new HttpMethodConstraint("DELETE") }
    );

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new {controller = "Home", action = "Index", id = UrlParameter.Optional}
    );

As best as I can tell -- I am getting my URL routed to my 'Get' action, but I am intended it to be routed to the 'GetPlaylistsByUserId' action. I believe this to be happening because the server error states that it is looking for a paramater 'id' for method 'Get.'

I'm not sure why any of this would be happening, though, as I seem to quite clearly have my other action mapped...?

Compare the route to the request - you're requesting an action name, which doesn't match the route. As it stands, your routing expects your ajax request to go to .../Playlist/SomeGuid as opposed to Playlist/GetPlaylistsByUserId?userId=SomeGuid .

If you want to route all requests to your Playlist controller to the GetPlaylistsByUserId action if no action is specified, the route you want is:

routes.MapRoute(
    "getPlaylistsByUserId",
    "Playlist",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

Note the omission of userId - this is passed as a querystring and doesn't need to be included in your route. MVC will bind this automatically.

As it stands, however, you are requesting an action name, so the following route will pick this up:

routes.MapRoute(
    "getPlaylistsByUserId",
    "Playlist/{action}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

You could also use {controller}/{action} but I am guessing you don't want all controllers to default to an action called GetPlaylistsByUserId .

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

I'm not sure if this could be it, but is there any chance it's because you don't have {action} in your Url pattern?

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{action}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

Your problem is that the Request url looks like

Playlist/GetPlaylistsByUserId?userid=51d77etc...

and your routing is set up as:

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

They don't match.

For this route to be used, the url should look like:

Playlist?userid=51d77etc...

or your url should stay the same and the route mapping should be

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{action}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);
routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

should be changed to

routes.MapRoute(
    "getPlaylistsByUserId",
    "{controller}/GetPlaylistsByUserId/{userId}",
    new { controller = "Playlist", action = "GetPlaylistsByUserId" },
    new { httpMethod = new HttpMethodConstraint("GET") }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM