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

(75 hits)
Hits=44 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;

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

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

      this.accountSummaries = result;


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


WebApi URI routing

namespace lssWebApi2.Controllers
    public class GeneralLedgerController : Controller
        public async Task<GeneralLedgerView> GetByAccountId(long generalLedgerId)
            GeneralLedgerModule glMod = new GeneralLedgerModule();
            return glMod.GeneralLedger.Query().GetLedgerViewById(generalLedgerId);

        // GET: api/<controller>
        public IEnumerable<AccountSummaryView> GetSummary(int fiscalYear)
            GeneralLedgerModule glMod = new GeneralLedgerModule();

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

            return list;

Additional references

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

Members : 152

Register to View