Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Client-side web app consuming Web API, how to populate select box field values based on expectations of server-side Web API?

I am working in a web application which has a server powered by ASP.NET Core MVC and the frontend is an Angular 5.0 Single Page Application using TypeScript.

In the client I have a page with a form and some <select> input controls. My design challenge here is that the choices in the <select> should be restricted to what values the Web API in my .NET Core expect.

Since the server and the client application are part of the same Visual Studio solution I wanted to try to minimize breaking changes in the Web API. One way I thought I might be able to do this is to have the client-side forms and inputs be somewhat dumb and to request from the server the valid values to populate the <select> controls with. This way I don't have to duplicate logic in the client and the server (For instance, if I defined an Enum in C# and again in TypeScript). There's a few down sides to this though that I can think of (like if I wanted to toggle visibility of other form fields based on the <select> value, I would have to hard-code logic in the client which makes assumptions about values.

Is this a good idea? What kind of other solutions could I use?

like image 279
Kyle V. Avatar asked Dec 15 '17 15:12

Kyle V.


People also ask

How do you handle a load in Web API?

Compress the Result of Web API In the Web, the data transfers through the network in packages (data packets), increasing the size of the data package, which will increase the size as well as increase the response time of the Web API. Thus, reducing the data packet size improves the load performance of Web API.

Which article in the fundamental section highlights all client side JavaScript elements and APIs?

Explanation: The Window object is the main entry point to all client-side JavaScript features and APIs. It represents a web browser window or frame, and you can refer to it with the identifier window.

What can be done with client side JavaScript?

The main tasks of Client side JavaScript are validating input, animation, manipulating UI elements, applying styles, some calculations are done when you don't want the page to refresh so often. In web developing it's the browser, in the user's machine, that runs this code, and is mainly done in javascript .

When building an action for an ASP Net core Web API controller What should be returned in case the operation completed successfully?

Returns an HTTP 201 status code if successful.


2 Answers

IF you are using ssr you can pass data to angular application from pre-rendering module.

In startup.cs

app.UseSpa(spa =>
        {
            ...
            spa.UseSpaPrerendering(options =>
            {
                ...
                options.SupplyData = (context, data) =>
                {
                    // Your c# array
                    data["selectValues"] = ["YOUR","ARRAY","OF","VALUES"];
                    // some other data
                };
            });

        });

Then in your main.server.ts file you can inject the array into angualar app.

import { createServerRenderer } from 'aspnet-prerendering';
...
const options = {
    document: params.data.originalHtml,
    url: params.url,
    extraProviders: [
      provideModuleMap(LAZY_MODULE_MAP),
      { provide: APP_BASE_HREF, useValue: params.baseUrl },
      { provide: 'MY_ARRAY_OF_VALUES', useValue: params.selectValues },
    ]
  };

Then inject it wherever you want in your application.

@Inject(MY_ARRAY_OF_VALUES) private selectValues:string[]

Not tested I am not sure it works with arrays.So you may want to change the supplied data format to JSON with Newtonsoft.Json and deserialize it in Angular application.

like image 162
Okan Aslankan Avatar answered Oct 09 '22 11:10

Okan Aslankan


One idea I got is to do something like:

  1. Use an Angular Service to call the Web API and get the possible choices (so it will be a list).
  2. Bind your possible choices list to a <select>.

Its pretty easy to make an Angular Service to do HTTP requests and get responses, you then put the possible choices in a variable in your component and then your View consumes that variable.

like image 26
Haytam Avatar answered Oct 09 '22 09:10

Haytam