locked
navigate based on a value in the controller action RRS feed

  • Question

  • User-1104215994 posted

    Hi,

    I have a web API. There is this action (PurchaseGame) in my controller. In the <g class="gr_ gr_132 gr-alert gr_gramm gr_inline_cards gr_run_anim Punctuation only-ins replaceWithoutSep" id="132" data-gr-id="132">request</g> <g class="gr_ gr_119 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling ins-del multiReplace" id="119" data-gr-id="119">productCode</g> comes and I wonder if the request <g class="gr_ gr_404 gr-alert gr_gramm gr_inline_cards gr_run_anim Grammar multiReplace" id="404" data-gr-id="404">navigate</g> to another action based on the value of productCode. I mean if the code is 000000007173, X action will be fired.

    [HttpPost, Route("purchase")]
    public async Task<IHttpActionResult> PurchaseGame(RequestDto game)

    Tuesday, October 15, 2019 8:37 AM

Answers

  • User475983607 posted

    Hi,

    I have a web API. There is this action (PurchaseGame) in my controller. In the request productCode comes and I wonder if the request navigate to another action based on the value of productCode. I mean if the code is 000000007173, X action will be fired.

    [HttpPost, Route("purchase")]
    public async Task<IHttpActionResult> PurchaseGame(RequestDto game)

    Actions are instance methods in controller class.  Add a basic if statement and call the action. 

    if(propertyValue == "0000007173")
    {
        var results = PurchaseGame(inputparameter);
    }

    IMHO, it seems you need to refactor code into a new method that can be called from two different Actions.  I recommend the reading through C# Programming Guide to learn how method work in C#; https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 15, 2019 11:11 AM