Translate

Saturday 25 March 2017

MVC Tips

  _ViewStart:

A) The _ViewStart file can be used to define common view code that you want to execute at the start of each View's rendering
B) Viewstart is used like Masterpage in traditional forms (ASP.Net pages).
C) Viewstart render first in the views.
D) Viestart is used to layout of the application.
E) Viewstart override all Views layout/template under "Views" folder in MVC .

For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default:

Proper way to use the file "ViewStart.cshtml":

The only other built-in place is that you can specify it from the controller -- one of the overloads to View() allows you to pass the layout template (the param is called master page).

Use, _VewStart.cshtml in each of the view folders, it doesn't mean that it is not DRY

The _VewStart.cshtml file contains following code.

@{
  Layout="~/Views/Shared/_Layout.cshtml";
}

General

• If you return PartialView() from your controllers (instead of return View()), then _viewstart.cshtml will not be executed.
• Assign a session in MVC:

Current.Session["UserName"] ="Guest";

• RedirectToActionPermanent() Method for which Status code represents? 300
• RedirectToAction() Method for which Status code represents? Ans. 301
• What is ActionResult() ? Ans: It is an abstract Class
• What is ViewResult() ? Ans:  It is a Concrete Class
• return View() works like in ASP.Net MVC C# as "Server.Transfer()".
• RedirectToAction() works like in ASP.Net MVC C# as "Response.Redirect()".
• In which format data can be return from XML into table ? Ans: Dataset
• Can we use view state in MVC ? Ans.NO
• Which Namespace is used for Razor View Engine ? Ans. System.Web.Razor
• Which Namespace is used for ASPX View Engine ?
Ans. System.Web.Mvc.WebFormViewEngine
• The Razor View Engine uses to render server side content. Ans. @
• The ASPX View Engine uses to render server side content. Ans. <%= %>
• Razor Engine supports for TDD, ASPX View Engine not support.

• If you have already implemented different filters then what will be order of these filters?

1) Authorization filters
2) Action filters
3) Response filters
4) Exception filters

• Can you specify different types of filters in ASP.Net MVC application?

1) Authorization filters (IAuthorizationFilter)
2) Action filters   (IActionFilter)
3) Result filters (IResultFilter)
4) Exception filters (IExceptionFilter)

• What is the significance of ASP.NET routing?

Ansr: Default Route Name:
"{controller}/{action}/{id}", // URL with parameters
By default routing is defined under Global.asax file. MVC ASP.Net uses routing to map between incoming browser request to controller action methods.

• Can be it possible to share single view across multiple controllers in MVC?

Ans: We can put the view under shared folder, it will automatically view the across the multiple controllers.

•MVC 6 Features:

We can merge MVC and Web API merged
new JSON project based structure
only save change, hitting the save but then refreshing the browser to reflect changes

• Return type of Controller action Method:

ViewResult - Renders a specified view to the response stream
PartialViewResult - Renders a specified partial view to the response stream
EmptyResult - An empty response is returned
RedirectResult - Performs an HTTP redirection to a specified URL
RedirectToRouteResult - Performs an HTTP redirection to a URL that is determined by the routing engine, based on given route data
JsonResult - Serializes a given object to JSON format
JavaScriptResult - Returns a piece of JavaScript code that can be executed on the client
ContentResult - Writes content to the response stream without requiring a view
FileContentResult - Returns a file to the client
FileStreamResult - Returns a file to the client, which is provided by a Stream
FilePathResult - Returns a file to the client

GET and POST methods with the same Action name in the same Controller

Since you cannot have two methods with the same name and signature you have to use the ActionName attribute:
    [HttpGet]
    public ActionResult Index()
    {
        Some Code--Some Code---Some Code
        return View();
    }

    [HttpPost]
    [ActionName("Index")]
    public ActionResult IndexPost()
    {
        Some Code--Some Code---Some Code
        return View();
    }

Friday 24 March 2017

Change stored procedure output column names and data types in SQL

Using WITH RESULT SETS to redefine column names and data types of stored procedure’s result set

Using WITH RESULT SETS option, we can redefine the metadata of result set(s) of a stored procedure during execution. We can use the below code to change the column names and data types of result sets(s) of stored procedure without making any change in the existing code:
1
2
3
4
5
6
7
8
9
10
11
12
13
EXEC [dbo].[uspGetEmployeeManagers] @BusinessEntityID = 101
WITH RESULT SETS
(
 (
 ReportingLevel INT,
 BusinessEntityID INT,
 FirstName NVARCHAR(50),
 LastName NVARCHAR(50),
 OrganizationNode NVARCHAR(MAX),
 ManagerName NVARCHAR(50)
 )
)
Here is the output of the stored procedure after redefining the column definitions using WITH RESULT SETS option:

ReportingLevel BusinessEntityID FirstName LastName OrganizationNode ManagerName 

Below is the syntax to define column names and data types in case we need to handle multiple result sets:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
EXEC ProcedureName @Param = 'Value'
WITH RESULT SETS
(
--Result set 1
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 2
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
),
--Result set 3
(
Column1 DataType,
Column2 DataType,
Column3 DataType
--Define all columns
)
--And so on
)

WITH RESULT SETS option – Limitations

  1. We cannot redefine a subset of columns – Either all columns of a result set need to be defined or none of them can be defined using WITH RESULT SETS option.
  2. Cannot redefine a subset of result sets – In case the stored procedure return multiple result sets, either all result sets (with all columns) need to be defined or none of them can be defined.
  3. Cannot change the order of the columns and result sets – We cannot change the sequence of columns and result sets.
  4. Cannot use calculation or type casting – We cannot use calculations or type casting with the columns.