0% found this document useful (0 votes)
53 views

Project Questions

The document discusses various C# concepts and .NET classes used in web development: 1. It lists several namespaces used for web APIs, data modeling, and asynchronous programming. 2. It describes classes like HttpClient used for sending HTTP requests, Task for representing asynchronous operations, and ModelState for validating form data. 3. It provides brief explanations of concepts like async/await for non-blocking code, DataAnnotations for validation attributes, and anti-forgery tokens for preventing CSRF attacks.

Uploaded by

Thimmana Gouda m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Project Questions

The document discusses various C# concepts and .NET classes used in web development: 1. It lists several namespaces used for web APIs, data modeling, and asynchronous programming. 2. It describes classes like HttpClient used for sending HTTP requests, Task for representing asynchronous operations, and ModelState for validating form data. 3. It provides brief explanations of concepts like async/await for non-blocking code, DataAnnotations for validation attributes, and anti-forgery tokens for preventing CSRF attacks.

Uploaded by

Thimmana Gouda m
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1. [DisplayName("Election ID")]:using System.

ComponentModel;
2. Key,Required,Datatype,DisplayFormat: using System.ComponentModel.DataAnnotations;
3. Column,DatabaseGenerated: using System.ComponentModel.DataAnnotations.Schema;
4. DbContext,DbSet: public object users { get; internal set; }
5. <IEnumerable<admin>:using System.Collections.Generic;
6. Task: using System.Threading.Tasks;
7. FirstOrDefaultAsync, ToListAsync,EntityState: using System.Data.Entity;
8. Httpclint: using System.Net.Http;
9. [HttpPost, ValidateAntiForgeryToken, ValidateInput(true)],ActionResult: using System.Web.Mvc;

 DatabaseGeneratedOption.none: The database does not generate values.


 DatabaseGeneratedOption. Identity: The database generates a value when a row is
inserted.
 DatabaseGeneratedOption. Computed: The database generates a value when a row is
inserted or updated.

HttpClient
we will use HttpClient class in console application to send data to and receive data from Web
API which is hosted on local IIS web server.

Method Name Description


GetAsync Sends a GET request to the specified Uri as an asynchronous operation.
GetByteArrayAsync Sends a GET request to the specified Uri and returns the response body as a byte array in an
asynchronous operation.
GetStreamAsync Sends a GET request to the specified Uri and returns the response body as a stream in an
asynchronous operation.
GetStringAsync Sends a GET request to the specified Uri and returns the response body as a string in an
asynchronous operation.
PostAsync Sends a POST request to the specified Uri as an asynchronous operation.
PostAsJsonAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value
serialized as JSON.
PostAsXmlAsync Sends a POST request as an asynchronous operation to the specified Uri with the given value
serialized as XML.
PutAsync Sends a PUT request to the specified Uri as an asynchronous operation.
PutAsJsonAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value
serialized as JSON.
PutAsXmlAsync Sends a PUT request as an asynchronous operation to the specified Uri with the given value
serialized as XML.
DeleteAsync Sends a DELETE request to the specified Uri as an asynchronous operation.

What is Asynchronous Programming?


Asynchronous programming is writing code that allows several things to happen at the same
time without "blocking", or waiting for other things to complete. This is different from
synchronous programming, in which everything happens in the order it is written (if you write
code for a living, chances are it will be synchronous code).

Let's look at a synchronous C# method:

public string GetNameAndContent()


{
var name = GetLongRunningName(); //Calls another webservice, can take up to 1
minute.
var content = GetContent(); //Takes up to 30 seconds
return name + ": " + content;
}

Every time something calls this method, the caller has to wait up to 1 minute before it can
resume processing. That's a minute of wasted time, time it could be spending doing other tasks.

public async Task<string> GetNameAndContent()


{
var nameTask = GetLongRunningName(); //This method is asynchronous
var content = GetContent(); //This method is synchronous
var name = await nameTask;
return name + ": " + content;
}

1. We marked the method as async. This tells the compiler that this method can run asynchronously.
2. We used the await keyword on the nameTask variable. This tells the compiler that we will
ultimately need the result of the GetLongRunningName() method, but we don't need to block on that
call.
3. We changed the return type to Task<string>. This informs the caller method that the return type
will eventually be string, but not right away and that can do other things
while GetLongRunningName() is processing.

Task: This class represents an asynchronous operation, and can be awaited.

Task<T>: This class represents an asynchronous operation that returns a value, and can be
awaited.

What is the role of async and await in Web API?


The async/await feature solves three performance or scalability problems: They can make your
application handle more users. If you have requests that access an external resource such as a database
or a web API then async frees up the thread while it is waiting.

What is task in async and await?

The async keyword turns a method into an async method, which allows you to use the await keyword in
its body. When the await keyword is applied, it suspends the calling method and yields control back to
its caller until the awaited task is complete. await can only be used inside an async method.

ModelState
A ModelState is a collection of name al value pairs submitted to the server duri POST request. It also
contains a collect error messages for each value. The Modelstate represents validation errors submitted
HTML form values.

HttpClient Class

Namespace: System.Net.Http
Provides a class for sending HTTP requests and receiving HTTP responses from a resource identified by a
URI.

What is ValidateAntiForgeryToken?

ValidateAntiForgeryToken is an action filter that can be applied to an individual action, a controller, or


globally. Requests made to actions that have this filter applied are blocked unless the request includes a
valid antiforgery token.
The basic purpose of ValidateAntiForgeryToken attribute is to prevent cross-site request forgery attacks.
A cross-site request forgery is an attack in which a harmful script element, malicious command, or code
is sent from the browser of a trusted user.

What is a task in C#?


A task is an object that represents some work that should be done. The task can tell you if the work is
completed and if the operation returns a result, the task gives you the result.

Why is async await used?

They allow you to write promise-based code as if it were synchronous, but without blocking the main
thread. They make your asynchronous code less "clever" and more readable. If you use the async
keyword before a function definition, you can then use await within the function. 12-Feb-2019
What is async task in C#?

C# asynchronous method is a special method that executes asynchronously.... It is used to perform


asynchronous tasks. C# await expression is used to suspend the execution of a method. If a method
which uses async modifier does not contain await expression, executes synchronously.

IEnumerable in C# is an interface that defines one method, GetEnumerator which returns an


IEnumerator interface. This allows readonly access to a collection then a collection that
implements IEnumerable can be used with a for-each statement.

In LINQ, AsQueryable operator / method is used to convert given input list elements to IQueryable<T>
list and AsQueryable is a method of System.

What is ModelState in Web API?


A ModelState is a collection of name and value pairs submitted to the server during a POST request. It
also contains a collection of error messages for each value. The Modelstate represents validation errors
in submitted HTML form values.

Enumerable.Empty<TResult> Method
Namespace:System.Linq
Returns an empty IEnumerable<T> that has the specified type argument.

Bind Attribute
ASP.NET MVC framework also enables you to specify which properties of a model class you want to
bind. The [Bind] attribute will let you specify the exact properties of a model should include or exclude
in binding.
What is bind attribute in MVC?
When we have an ASP.NET MVC View that accepts user input and posts those inputs to a server we have
the option to use the built-in Model-binding features to provide more control and security, We can
restrict the properties that are allowed to be bound automatically.

When to use [FromBody] and [FromUri] in Web API

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the
URI of the request, and the [FromBody] attribute is used to specify that the value should be read from
the body of the request. ... This is the default behavior of Web API.

What is FromBody in C#?


When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this
example, the content type is "application/json" and the request body is a raw JSON string (not a JSON
object). At most one parameter is allowed to read from the

AntiForgeryToken Method (System. Web. Mvc) Generates a hidden form field (anti-forgery token) that
is validated when the form is submitted.

What is HTML AntiForgeryToken ()?


A great feature in ASP.NET MVC is the AntiForgeryToken. This Generates a hidden form field (anti-
forgery token) that is validated when the form is submitted. The anti-forgery token can be used to help
protect your application against cross-site request forgery.

BeginForm extension method in ASP.NET MVC5. ActionMethod – It defines which action method is look
for when submit button is clicked. ControllerName – It defines, which controller keeps the defined
action method. Get/Post Method – it defines the method you want to use to send data from form to
controller.

What is Form-Group bootstrap?


The . form-group class is the easiest way to add some structure to forms. It provides a flexible class that
encourages proper grouping of labels, controls, optional help text, and form validation messaging. By
default it only applies margin-bottom , but it picks up additional styles in . form-inline as needed.

What is form control bootstrap?


Form controls automatically receive some global styling with Bootstrap: All textual <input> , <textarea> ,
and <select> elements with class . form-control have a width of 100%.

What is a well bootstrap?


A bootstrap well is something like a bootstrap panel with round borders and padding around it. This is
used to create some attention towards some content of the webpage. The . well class adds a rounded
border around the element with a gray background color and some padding.

What is the use of TestFixture?


This is the attribute that marks a class that contains tests and, optionally, setup or teardown methods.
Most restrictions on a class that is used as a test fixture have now been eliminated.

The [TestFixture] attribute at the beginning indicates that this class is a test fixture so that NUnit can
identify it as a runnable test class. ... SetUp method is run at the start of test fixture and TearDown
method is run at the end, after running all the test cases in the test fixture.

What is NUnit test?


NUnit is an open-source unit testing framework for the . NET Framework and Mono. It serves the same
purpose as JUnit does in the Java world, and is one of many programs in the xUnit family.

Setup
This attribute is used inside a TestFixture to provide a common set of functions that are performed just
before each test method is called. SetUp methods may be either static or instance methods and you
may define more than one of them in a fixture.

What is assert in C# unit test?


The Arrange section of a unit test method initializes objects and sets the value of the data that is passed
to the method under test. The Act section invokes the method under test with the arranged parameters.
The Assert section verifies that the action of the method under test behaves as expected

You might also like