Is it possible to change the querystring variable in ASP.NET MVC path before it hits the controller?

后端 未结 4 879
甜味超标
甜味超标 2021-01-03 06:26

I have a controller method in ASP.NET MVC that looks like this:

public ActionResult GetAlbumPictures(int albumId)
{
    var album = AlbumRepo.GetSingle(album         


        
4条回答
  •  心在旅途
    2021-01-03 06:35

    You can create a custom action filter attribute to handle this scenario. I haven't tested this specific implementation, but the general idea is to do something like this:

    public class AlbumAttribute : ActionFilterAttribute
        {
             public override void OnActionExecuting(ActionExecutingContext filterContext)
             {
                 var albumId = filterContext.HttpContext.Request.QueryString["album"] as string;
                 filterContext.ActionParameters["albumId"] = albumId;
    
                 base.OnActionExecuting(filterContext);
             }
        }
    

    Then, decorate your action method with the [Album] attribute:

    [Album]
    public ActionResult GetAlbumPictures(int albumId)
    {
        var album = AlbumRepo.GetSingle(albumId);
        var pictures = album.Pictures;
        return View(pictures);
    }
    

提交回复
热议问题