I’m kind of new to APIs and currently working on a project using the Spoonacular API. Im trying to build a meal planner website. At the moment, I’m working on the “Add To Meal Plan” feature which is a post method. To get the information for the post method, I think have to use a get method to retrieve recipe information from the API using a Recipe ID. I’m using TempData to store the information I get back from the get method so I can use it in my post method. Is this the most efficient way to be doing this? Or is it better to have my get and post requests be in the same method so I don’t have to store anything?
Currently, I’m using TempData to store the recipe information. It works but just not sure if this is the most efficient way to do this. I’m storing an object that I’ve serialized.
Using TempData is an acceptable approach for storing temporary data that needs to be passed from one request to another. It's part of the ASP.NET Core framework and provides a simple way to store values between multiple requests.
However, depending on your specific use case, there might be other ways to store this information that are more efficient. For example, if you want to store the recipe information for multiple users and persist it between sessions, you could consider using a database or a caching solution.
If the information you're storing is specific to a single user session, then TempData might be the best choice as it's relatively lightweight and easy to use.
In terms of combining the get and post requests into a single method, it's not necessary to do so. However, it might be more efficient in terms of network requests and server load if you can reduce the number of requests that need to be made to retrieve the required information.
Tell me if this helps :)
Related
Some background - I am working in a project which requires a kind of headshake authentication. The external service will send a request with a Token, and I will answer with Validator. Then it will send a second request containing the same Token and the data I should store in my database. The token is also used to get a couple extra fields that are required to insert the data in the database. Due to several project constraints and requirements, this "api" is implemented in Serverless (Azure Functions).
Since there are only 100 and something token-validator pairs that are not often updated (I will update manually every month or so), I have decided about not querying the database every time I get an incoming request. Normally I would simply use caching in C#, but since I am working with Functions, the code will be executed in multiple changing processes, which means no shared cache. I also think that using a cache service, such as Redis or Azure Cache would be an overkill.
My current solution - Currently, I am storing the data in a Hashtable that maps a Token to a ValidatorModel object that contains the validator and the extra fields I require. It works pretty well, but since it is a big C# object, it is a pain to update, the IDE lags when I open it, etc. I also don't know if it is a good idea to have it hardcoded in C# like that.
What I have been thinking about - I was thinking about storing a binary protobuf file that contained my Hashmap object. I am unsure if this would work or perform well.
My question - What is the best way do store such data and access it in a performatic way?
I have client requirement in which client wants to get output data of bio metric machine and want to process in asp.net c#. How to get Data from bio metric machine and process int asp.net?
Your question is very generic so here's a very generic answer.
Generally speaking processing data in asp.net consists of:
Creating controllers with HttpGet, HttPost methods
Saving data into a data store (e.g. a database)
There are many questions you need to ask before you come up with a good design. For example:
How much data?
How often?
How would the data be consumed (via an API by a 3rd party app?, published somewhere?
Would you be using an existing datastore?
What authentication and authorization requirements need to be met?
(Many, many more questions.)
One place to find samples for aps.net is https://github.com/aspnet/samples
As an example, if your data comes in as json, you could look at JsonUploadSample:
This sample illustrates how to upload and download JSON to and from an ApiController.
Good luck!
For a website, consider this scenario:
Users input data, including a ID, and submit
A related controller will handle the request. At this point, one complex object will be created (by calling web services, with the ID) for a series of operations. e.g validation, etc
Return user a specific View
User input another set of data and submit
Another controller will handle this request. It requires the complex object used in step 2.
and so on...
Currently, in step 5, I will use the ID to call the web service again for getting the required complex object.
Is there any correct/efficient way, without using TempData/ViewBag/ViewData, so that I can reuse the complex object, once it is created, in step 2?
Edit:
Session is not allowed as well.
Normally, for web application, there're multiple options to store complex object depending on your need. I don't think there is a BEST way of doing, only the most suitable way and every solutions will come with PROS and CONS
SERVER SIDE
Session (I know you said cannot use session, but I just want to include it anyway): first option comes to mind, suitable for most web application. Since the modern web development are more on STATELESS, a lot of people want to avoid using Session at all cost. However, there're some specific infrastructure configuration to support session in STATELESS application such as distributed session or sticky session or you can save the session in a dedicated server or database.
PROS: easy to use, support web application naturally
CONS: need to configure a lot of things to work with STATELESS application
ANOTHER Dedicated server (Before anyone ask, I put it in the SERVER SIDE section even though it's another SERVER, but to me, whatever in our control is SERVER SIDE): a few options for you to choose here, first option could be to set up a cache server (Redis?) and retrieve/save using key (similar to session), or you can simply write an application to retrieve/save using your own logic.
PROS: reusability, extendability, works with all applications not just web, have its own scope
CONS: difficult to setup
Database: not a obvious choice, but database do support this kind of requirement
PROS: reusability, extendability, works with all applications not just web
CONS: performance issue
Other in-memory options (TempData, ViewBag, etc):
PROS: easy to use, well-supported with ASP.NET MVC
CONS: somtimes it's hard to pass around multiple views
CLIENT SIDE
There are so many options here to choose like use hidden fields, cookie, localStorage, sessionStorage, etc or even a simple query string will work
PROS: speed (since you don't need client-server transportation)
CONS: security (you cannot trust anything from client-side), not doing well with too complex object (heavy object), security (sensitive data), etc
SUGGESTED SOLUTION
I hope I understand your issue correctly but in my opinion, you should not store complex object, simply store the ID of the complex object in place of your choice, and make a query every time you need the object. So that your object is always up-to-date and you don't waste resource to store the complex object.
Hope it helps you.
If you want to project an object to a certain view, then forget about any storage (TempData/ViewBag/ViewData/Session) and then post the same object to another controller, the best way you can re-construct your object is to store the object's properties as hidden <input> controls.
I've been everywhere trying to find an answer to this so hopefully someone can help.
A little bit of background: I'm getting my feet wet with SignalR and attempting to expand on the Chat Room Example. My idea is basically to have a list of users who have entered the room. I am attempting to do this by storing a list of usernames in a "users" cache key and maintaining and distributing this list when users login/logout. I want to access this list from within my hub's code when requested by the client.
When I use Application I get the desired results but it is my understanding that HttpContext.Current.Application is not the best way to go as it is only there for compatibility with classic asp. I see a lot of recommendations for HttpContext.Current.ApplicationInstance, however, whenever I access this I get a NullPointerException on ApplicationInstance. What is the best way to manipulate some cache keys (or an equivalent) for what I am trying to accomplish?
I wouldn't recommend using Application for this at all. If you simply want to cache and keep it up to date based on events such as login/logout, use a static member on your Hub class or object cache (e.g. http://msdn.microsoft.com/en-us/library/dd997357.aspx)
Use a database, cache or a static variable to store your state. Avoid using HttpContext.Current inside of SignalR.
I am doing my first ASP.NET MVC project. (In fact, for the record, this is my first production website of any kind).
This is a mobile web page targeting HTML 5.
This page looks up some "expensive" information. First it uses the html 5 geocoding feature to get the customers latlong from their browser.
I pass that information to a controller. That controller fills in the City and State (into a location model) using the Google Maps API and then uses it in the view.
So this data is expensive in two ways, first it takes time to look it up and second, in the case of the Google API, it is literally not free in that Google limits the number of calls that can be made per day.
I understand that MVC is designed to "embrace" the web including the fact that http is a stateless protocol.
Nevertheless, I would like to avoid having to fetch this expensive data on every page load for every endpoint that needs it. Furthermore, one other piece of state that I would like to keep track is the fact that the data has already been fetched.
What is the best way / best practice for achieving this on an MVC 3 web application? Since my model for location has 4 data point (lat long city state) and there is a fifth data point (data retrieved) I would really like to avoid using querystrings to do this or a route for all of those data points?
I am sure this is a common need but I honestly don't know how to tackle it. Any help will be appreciated.
Seth
It Seems to me that you would like to cache the API call to google.
http://msdn.microsoft.com/en-us/library/18c1wd61(v=vs.71).aspx
You can store the object you got from google in cache and call it on the new controller event. you could also create another object that has the object from google and a bool that indicates if you have fetched the data or not.
It seem to me that the Cache would be your best bet.
You can store it in session state if it is available in your case instead of passing between pages.
Since this is "expensive" data, but still not to be persisted for a long time, you may:
use Session state
put the data in the Cache and either
set a cookie to enable the retrieval of the "expensive" data from cache
use a cache key which is unique to each query (lat long city state ?)
store the data ("data retrieved") on the client (since you do not seem to persist it on the server side)
My personal preference would be server side cache with a unique key.
Store expensive data to the cache, and build cache ID by parameters you send to google, cache id should be unique for every distinct place
Another option would be html5 storage. You will want to check to see if your target browsers support it though. The advantage to this approach is that the server does not have keep track of this data in session or a database - in fact the server doesn't know about client storage at all.
try
Session[xxx]=xxx;
or
Application[xxx]=xxx;
instead