ASP.NET MVC - passing parameters to the controller

后端 未结 10 1328
小蘑菇
小蘑菇 2020-11-28 23:36

I have a controller with an action method as follows:

public class InventoryController : Controller
{
    public ActionResult ViewStockNext(int firstItem)
           


        
10条回答
  •  没有蜡笔的小新
    2020-11-29 00:09

    Headspring created a nice library that allows you to add aliases to your parameters in attributes on the action. This looks like this:

    [ParameterAlias("firstItem", "id", Order = 3)]
    public ActionResult ViewStockNext(int firstItem)
    {
        // Do some stuff
    }
    

    With this you don't have to alter your routing just to handle a different parameter name. The library also supports applying it multiple times so you can map several parameter spellings (handy when refactoring without breaking your public interface).

    You can get it from Nuget and read Jeffrey Palermo's article on it here

提交回复
热议问题