|
Hope someone can share some knowledge.
I made a HttpPost method, which sent a list of ids to a method in my service. The method then compared the ids to a anothers list, and return the object, which does´t contain the ids from the post request.
Now I want to make a HTTP Get method, which get the object after they are sorted
<pre> public List<ProductBO> GetAll()
{
using (var uow = facade.UnitOfWork)
{
return uow.ProductRepository.GetAll().Select(p => Pconv.Convert(p)).ToList();
}
}
public List<ProductBO> FilteretProduct(List<int> ids)
{
var AllProducts = GetAll();
List<ProductBO> FilteredProducts = new List<ProductBO>();
foreach (var prod in AllProducts)
{
foreach (var id in ids)
{
if (!prod.IngredientIds.Contains(id))
{
FilteredProducts.Add(prod);
}
}
}
return FilteredProducts;
}
<pre>
[HttpPost]
[Route("FilteredProducts")]
public List<ProductBO> FilteredList(List<int> ids)
{
List<ProductBO>filterProduct;
return filterProduct = facade.ProductService.FilteretProduct(ids);
}
[HttpGet]
[Route("GetFilteredProducts")]
public List<ProductBO> GetFilteredList()
{
return filterProduct;
}
|
|
|
|
|
That doesn't make any sense. Web applications are stateless - they don't remember state from one request to the next. So even if you moved your local variable out of the POST method to make it available from the GET method, by the time you made the GET request, the content of the variable would have been lost.
You would need to use some form of session storage to persist the data between the requests. The precise details of how you do that will depend on how you're hosting your application, and whether you're using the full framework or .NET Core.
It's not clear why you would need two requests, when the POST request already returns the data?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
thanks for the replay!
Normally I make a post request to insert some values and af get request to get some values.
I didn´t know you could do that with a post request ?
|
|
|
|
|
i have two interface with diffrent name but method name will same and i am consumming in a class both interface how i know which method will cll i1 method or i2 method
|
|
|
|
|
What you are looking for is the difference between an implicit and explicit interface. There's a very good explanation here[^].
This space for rent
|
|
|
|
|
How to make Voting system in C# that should be object oriented programming?
|
|
|
|
|
Gather the requirements, identify the inputs and outputs, design the user interface, and write and test the code.
|
|
|
|
|
first each user should save ther self with fingerprint scanner and then can vote to the candidates.
candidates must have photo and name
|
|
|
|
|
|
Those aren't requirements; that's a vague wishlist. For instance, what types of photo id are you accepting? What happens if the fingerprint scanner breaks down? How are the candidate details stored? How do you vote for them?
This space for rent
|
|
|
|
|
And?
What have you done so far?
Where are you stuck?
What help do you need?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
OriginalGriff wrote: What help do you need?
Well duh - send the codez NOW!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
That was my guess: "I want you to do my homework" but it'd be nice to know if he's genuine and wants to think or just waste his whole life ...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Democracy's in danger!
"I'm neither for nor against, on the contrary." John Middle
|
|
|
|
|
I'd like to start using more C# generics. Can someone tell me what the
<?> portion of the method syntax below represents? Is it a placeholder for the object type? (see my example further down)
<Access Specifier> <Return Type> <Method Name><?>(Parameter List)
{
Method Body
}
I was wanting to do something like this, so that I could pass either an OLEDB DB Connection object or an SqlClient connection object.
private T DataConnection<T> ( T db_conn_obj , int index_of_mydatafile )
{
...
}
|
|
|
|
|
The "bit in the middle" is the indicator that this is a Generic method, and allows you to specify a substitution name which identifies where the generic class type is needed. It doesn't have to be <T> - you can use any valid identifier:
private TypeOfClass DataConnection<TypeOfClass>(TypeOfClass db_conn_obj, int index_of_mydatafile)
{
return db_conn_obj;
}
But the convention is to use "T" as the generic class name.
The trouble with that is I can happily pass any class type to your method:
var x = DataConnection("hello", 666); And it will work - additionally, You can't use any of the fields, properties, or methods of your Connection, even if you pass an SqlConnection object.
The solution is to apply a constraint:
private T DataConnection<T>(T db_conn_obj, int index_of_mydatafile) where T: System.Data.Common.DbConnection
{
Console.WriteLine(db_conn_obj.ConnectionString);
db_conn_obj.Open();
return db_conn_obj;
}
Which limits the classes you can pass to those derived from DbConnection (which includes SqlConnection, OleDbConnection, and so forth:
var x = DataConnection(new SqlConnection(), 666);
var y = DataConnection(new OleDbConnection(), 666);
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This is a big help! I've been working on it for hours. I'm working on some existing code that I'm supposed to make work with both OleDbConnection as well as SqlConnection.
It's more of a training exercise for me, because it looks like it's going to take a lot of time to convert all of the db methods to use either connection.
So right now, I have an OleDbConnection, like so:
private OleDbConnection DataConnection ( int index_of_mydatafile )
{
string dbpath = MyDataFiles[ index_of_mydatafile ];
string conn_string = OledbProvider(dbpath) + "data source=" + dbpath;
Debug.WriteLine ( conn_string );
return new OleDbConnection ( conn_string );
}
The connection is just be declared and instantiated in a using statement like this:
using ( OleDbConnection dbConnection = DataConnection ( index_of_mydatafile ) )
using ( OleDbCommand dbCommand = new OleDbCommand ( sql , dbConnection ) )
using ( OleDbDataAdapter adapter = new OleDbDataAdapter ( dbCommand ) )
So the command object is actually opening the connection later. To get this working, I tried this, but I'm stuck at the new declaration, which is causing an error, presumably because I need to cast the T maybe? I'm just pulling things out of thin air now. lol
private T DataConnection<T> ( T db_conn_obj , int index_of_mydatafile ) where T : System.Data.Common.DbConnection
{
string dbpath = MyDataFiles[ index_of_mydatafile ];
string provider = OledbProvider ( dbpath );
string conn_string = provider + "data source=" + dbpath;
Debug.WriteLine ( conn_string );
if ( provider == "" )
{
conn_string = @"Data Source= xxxx\xxxx;Initial Catalog=xxxx;Integrated Security=SSPI;Connect Timeout=5;ConnectRetryCount=0;ApplicationIntent=ReadWrite;"
}
return new T ( conn_string );
}
|
|
|
|
|
Ah! To create a new instance, you need to tell it that the only classes that can be "generic-ed" are concrete classes (like OldedbConnection, and SqlConnection), not abstract classes like DbConnection.
That's easy: add the new constraint (and remove the class instance from the parameters list):
private T DataConnection<T>(int index_of_mydatafile) where T : System.Data.Common.DbConnection, new()
{
T db_conn_obj = new T();
db_conn_obj.ConnectionString = "...";
db_conn_obj.Open();
return db_conn_obj;
}
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Holy crap, man! That's freak'n awesome! It's a double ++ because the original code doesn't include the class instance in the parameters list.
This is the first time I actually posted a question on any programmer's forum. I wish I had done this sooner.
I really appreciate it.
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
RickBishop wrote: I'm supposed to make work with both OleDbConnection as well as SqlConnection.
Could be done fairly easy without generics. See A short tale on two patterns that live in .NET[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
This is very applicable. I will take a look. Thanks for sharing!
|
|
|
|
|
You're welcome, and thanks
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
What do you need, and what do the connections have in common?
You will find that OldDbConnection and SqlConnection (among others) inherit from Common.DbConnection (which also has an interface).
You can pass a class instance that another class "inherits" from (or an interface) if it meets your requirements instead of having to resort to generics (in this case).
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I'm very new to Active Directory, so bear with me please...
First
I'm using the following to validate a user:
public bool ValidateUser(Credentials credentials, string userName = "", string password = "")
{
using (var context = new PrincipalContext(ContextType.Domain, credentials.ServerName, credentials.UserName, credentials.Password))
{
return context.ValidateCredentials(userName, password);
}
}
But the ValidateCredentials takes the password in PLAIN TEXT and it works fine. This doesn't seem right. How is encryption handled? How do you encrypt the text and how does AD know the PW is encrypted?
Second
Next, I know that LDAP is a protocol. From what I see you specify LDAP in the
query string, as in "LDAP://OU=staffusers,DC=leeds-art,DC=ac,DC=uk"
Is this the way it's done? How do you specify another protocol to use?
Thanks
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|