Есть ли порядок аннотаций данных для ASP.Net MVC Controller?

Я нашел что-то странное, и мне нужно профессиональное мнение.. У меня есть приложение ASP.NET MVC имеет представления для данных с сортировкой, поиском и разбиением на страницы. и я использовал аннотацию данных MultipleButton, чтобы я мог выполнять несколько действий с такими же параметрами, как указано ниже:

    [SessionAuthorize]
    [AuthorizationFilter(Page = PageEnum.EgyptianEmployees, PageAction = PageActionEnum.View)]
    public ActionResult Index(string sortOrder, string searchString, string currentFilter,int? searchType , int? filtertype, int? page)
    {
        //Normal View index with filter, sorting, and pagination
    }




    [SessionAuthorize]
    [AuthorizationFilter(Page = PageEnum.EgyptianEmployees, PageAction = PageActionEnum.View)]
    [MultipleButton(Name = "action", Argument = "IndexSearch")]
    public ActionResult IndexSearch(string sortOrder, string searchString, string currentFilter, int? searchType, int? filtertype, int? page)
        {
            return RedirectToAction("Index", new { sortOrder = sortOrder, searchString = searchString, currentFilter = currentFilter, searchType = searchType, filtertype = filtertype, page = page });

        }


      [SessionAuthorize]
      [MultipleButton(Name = "action", Argument = "IndexReport")]
      [AuthorizationFilter(Page = PageEnum.EgyptianEmployees, PageAction = PageActionEnum.View)]
       public ActionResult IndexReport(string searchString, int? searchType)
        {
         //Some Reporting Technique 
         }

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleButtonAttribute : ActionNameSelectorAttribute
{
    public string Name { get; set; }
    public string Argument { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        var isValidName = false;
        var keyValue = string.Format("{0}:{1}", Name, Argument);
        var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);

        if (value != null)
        {
            controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
            isValidName = true;
        }

        return isValidName;
    }


}

И на мой взгляд было так:

@using (Html.BeginForm("", "EgyptionEmployee", FormMethod.Get))
            {

                <div class="col-lg-12">
                    @Html.DropDownList("searchType", (SelectList)ViewBag.SearchList, new { @class = "form-control" })
                </div>
                <hr />
                <div class="col-lg-12">
                    <div class="input-group" dir="ltr">

                        <input type="text" id="SearchString" value="@ViewBag.CurrentFilter" name="SearchString" class="form-control" dir="rtl" placeholder="Search ...">
                        <span class="input-group-btn">

                            <button class="btn btn-secondary" type="submit" value="Search" name="action:IndexSearch">Search</button>
                            <button class="btn btn-primary" type="submit" value="Report" name="action:IndexReport">Download Search Report</button>


                        </span>
                    </div>
                </div>
            }

Но я получил и ошибку:

Текущий запрос на действие "Индекс" для типа контроллера "ForignEmployeeController" неоднозначен между следующими методами действия: System.Web.Mvc.ActionResult IndexSearch(System.String, System.String, System.String, System.Nullable'1[System.Int32], System.Nullable'1[System.Int32], System.Nullable'1[System.Int32]) для типа Tourism.Controllers.ForignEmployeeController System.Web.Mvc.ActionResult Index(System.String, System.String, System.String, System.Nullable'1[System.Int32], System.Nullable'1[System.Int32], System.Nullable'1[System.Int32]) для типа Tourism.Controllers.ForignEmployeeController

После нескольких часов борьбы

Я просто заменил порядок аннотаций данных. Я поставил MultipleButton перед AuthorizationFilter следующим образом:

[MultipleButton(Name = "action", Argument = "IndexSearch")]
[AuthorizationFilter(Page = PageEnum.EgyptianEmployees, PageAction = PageActionEnum.View)]

Мой вопрос заключается в том, почему это решило проблему и существует ли какой-либо порядок типов, которому я должен следовать в будущем, чтобы избежать ошибок такого рода?

0 ответов

Другие вопросы по тегам