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?
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.
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.
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 .
Returns an HTTP 201 status code if successful.
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.
One idea I got is to do something like:
<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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With