Scott Harrison - The golden apples
Henry Parker's Robot Wars

ASP.Net, ADO, MS Access
C# - How to override webapi uri routing

C# - How to override webapi uri routing

(5 hits)
Hits=6 Overview: In this code snippet, I demonstrate how to override an api route. It can be confusing, how to override the route. At the controller level the route is defined as api/[controller] and by convention the controller is the name of the class, in this case, "GeneralLedger". In my angular component, I make a get call using the path api/GeneralLedger(the controller)/BySummary/2018 and await results. The Web api maps to the controller and the uri routing BySummary with the parameter fiscal year, [Route("BySummary/{fiscalYear}")]. The parameter is received an the dataservice calls the fluent module with its unit of work domains to extract the data from the sql server database.

Angular Component

export class GeneralLedgerComponent {
  public accountSummaries: IAccountSummaryView[];
  public mystring: string;

  constructor(
    http: HttpClient, @Inject('BASE_URL') baseUrl: string) {

    http.get<IAccountSummaryView[]>(baseUrl + 'api/GeneralLedger/BySummary/2018').subscribe(result => {

      this.accountSummaries = result;

        //console.log(result);


    }, error => console.error(error));


  }
  
 }

WebApi URI routing


namespace lssWebApi2.Controllers
{
    [Route("api/[controller]")]
    public class GeneralLedgerController : Controller
    {
  
       
        [Route("ById/{generalLedgerId}")]
        [HttpGet]
        public async Task<GeneralLedgerView> GetByAccountId(long generalLedgerId)
        {
            GeneralLedgerModule glMod = new GeneralLedgerModule();
            return glMod.GeneralLedger.Query().GetLedgerViewById(generalLedgerId);
        }

       
        // GET: api/<controller>
        [Route("BySummary/{fiscalYear}")]
        [HttpGet]
        public IEnumerable<AccountSummaryView> GetSummary(int fiscalYear)
        {
  
            GeneralLedgerModule glMod = new GeneralLedgerModule();

            IEnumerable<AccountSummaryView> list = glMod.GeneralLedger.Query().GetAccountSummaryByFiscalYearViews(fiscalYear);

            return list;
        }
}

Additional references

https://docs.microsoft.com/en-us/aspnet/web-api/overview/web-api-routing-and-actions/attribute-routing-in-web-api-2

https://angular.io/guide/forms
....

...<<<Register to correspond>>> ...

Members : 152
Name:
Email:

Register to View

Help