0

I have an ASP.NET Core 3.1 Web API project. There is an enum with the following details:

public enum TestPageType
{
    Item1,
    Item2,
    Item3,
    Item4
}

[HttpGet("{type}/{mode}")]
[ProducesResponseType(400)]
[ProducesResponseType(500)]
public IEnumerable<ControlInfo> GetControlData(TestPageType type, string mode)
{
    switch (type)
    {
        case TestPageType.Item1:
        case TestPageType.Item2:
        case TestPageType.Item3:
        case TestPageType.Item4:
        default:
            return _testService.GetData(GetControlType(type), 0, readonlyMode);
    }
}

From the client application, Item5 is sent as input to the API endpoint:

https://localhost:8080/api/test/GetControlData/item5/edit

In this on debugging I see that first item from the TestPageType enum : Item1 is picked.

Can anyone help me to resolve this issue?

4
  • Have you tried with https://localhost:8080/api/test/GetControlData/Item5/edit (Item5 with a capital I) - the parameter values might be case-sensitive .... Commented Aug 29, 2022 at 13:29
  • Thanks @marc_s for your reply. I did it. Infact I tried to validate it wrong entry like abcd which is not there in the list. In that case it is picking the first item of the enum. Not sure why this is happening in this case. Any help on this request is much appreciated. Commented Aug 29, 2022 at 13:31
  • Item5 doesn't exist in your enum, and since an enum is a value type, it will probably default to 0 or Item1. You could try with TestPageType? type as the parameter perhaps? Commented Aug 29, 2022 at 13:33
  • Thanks @DavidG for your response. I will give a try and will keep this post here. Commented Aug 29, 2022 at 13:36

1 Answer 1

1

By default, the associated constant values of enum members are of type int; they start with zero and increase by one following the definition text order. And the default value of an enumeration type E is the value produced by expression (E)0, even if zero doesn't have the corresponding enum member. More detail information, see Enumeration types (C# reference).

In your code, since the TestPageType doesn't has the Items5 item, so it will use the default value (Item1).

To receive the not exist item: "Item5", you can try to change the parameter type from TestPagetType to string type. Then check whether the value is the enum value.

Code like this:

    [HttpGet("{type}/{mode}")]
    [ProducesResponseType(400)]
    [ProducesResponseType(500)]
    public IActionResult GetControlData(string type, string mode)
    { 
        var selectitem = "";
        switch (type)
        {
            case "Item1" :
                selectitem =  TestPageType.Item1.ToString();
                break;
            case "Item2":
                selectitem = TestPageType.Item2.ToString();
                break;
            case "Item3":
                selectitem = TestPageType.Item3.ToString();
                break;
            case "Item4":
                selectitem = TestPageType.Item4.ToString();
                break;
            default:
                selectitem = "no exist";
                break;
        }

        return Ok(selectitem);
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @Zhi Lv for your response. It helped me to fix the issue :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.