NET Core 7 MIME Types ContentResult PhysicalFileResult vs VirtualFileResult vs FileContentResult

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign in this lesson we will continue practicing controllers we will explore the available options for implementing the content response mime types such as text HTML Json and binary in netcore 7 and discuss methods like content result content physical photos of virtual file result and file content result mime stands for a multi-purpose internet mail extensions it's a standard that extends the format of ml messages to support text in character sets other than ASCII as well as attachments of audio video images and applications programs a mime type in netcore 7 is a string that identifies the type of data being transmitted it's a two-part identifier consisting of a type and subtype the type specifies the general category of the data such as an application image or text and the subtype specifies the specific format of the data such as Json jpeg or HTML my types are used by web servers and browsers to determine how to handle the data being transmitted as you may recall from previous lessons when the server sends a response to the browser it includes some necessary information but this information includes details like the response status server type the type of response whether it's text HTML Json Etc the date and more additionally it's important to know that we can send a different content type in the response that what was requested by the client for instance if the client asks for text the server can respond with HTML or any other format we are not bound to respond in the exact format requested by the client now let's get back to our code to send the response of a specific type we need to include this technical data in the server's response so if I declare this type using content result then by using options like content and content type we can specify the response that the server will provide if I compile and open the block address in the browser the responsible match the code will be defined and in the browser's devtools you will see that the content type is set to text plane so in this case the content is that actual text which is a string and each type is plain text it's important to note that this type setting is specific to the blocking point it doesn't mean that all endpoints need to have the same type in fact for any other endpoints the type can be different for example I can change the home URL to have an HTML type and now you will see that the type is HTML and default styles are applied additionally we can directly add HTML style select textiles or font sizes in the response along with the included H1 tag now we have an endpoint that explicitly returns HTML and another one that explicitly returns plain text however this approach can be a bit cumbersome there is an alternative approach we can use by inheriting from MVC control so home control will inherit from Microsoft asp.core MVC controller and for the user endpoint we can make it shorter by using only content and since we have declared the use of MVC in the file header this part can be left out and we can declare a controller only now if you compare these two records block end users you will notice that the one for the user endpoint is much cleaner and shorter if you open the content method definition you will see that content is already provided to us and it returns a Content result object type each parent class is controller base a which we will delve into further in the second part of the controller's lessons where we will work on a project involving SQL and Swagger if you open the definition for the controller class you will see that the controller Base Class is also listed here so to recap we have inherited from a controller and within that we have the controller Base Class which contains the content method returning a Content result object that's why we include the using Microsoft asp.core MVC declaration and our home controller class automatically inherits from a controller class with the content method now what we need to do is simply use the content method to make our code more readable and Visually appealing and in the next example we will work with Json format Json short for JavaScript object notation is a lightweight way to exchange structured data it's often used to represent data objects with attributes and arrays Json is easy for humans to read store and transmit in this example we have a Json top level object with two nested objects at person and address the format is referred and we won't dive deep into it in this lesson we will cover Json format in more detail in future lessons now I will add an additional endpoint and name it GB access now this endpoint will return Json data first we will create a simplified version of this endpoint we just need to create a simple object convert it into Json format and then use the content method to return it as Json as a result the endpoint will reply in Json format and in these developer tools you will see that the response header is of Json type as well and to clarify how to respond with the class object I will quickly add a class called access in this class I will include a few properties login password ID and off and what is happening here is quite straightforward since you are familiar with c-sharp programming as this simulates database access and we will relocate this class into a folder named model following MVC model view controller pattern in the home controller we will require the namespace and I will include an extra endpoint the remaining code is straightforward c-sharp so I won't provide comments for it when we access the endpoint of we will receive a proper response but that includes a generated guid in this lesson we will also talk about a file result whenever you have a file that can be downloaded like a PDF or a binary installation file you need to declare it as a file result there are primarily three different methods for managing and delivering files the first one is the physical file result which provides a file from a physical file system to a client the file path must be an absolute path a virtual file result delivers a file from a virtual file system to a client the file path can be a relative path a file content result Returns the content of a file to a client regardless of the files location the file content can be in any form such as text images or audio it doesn't require the file to actually exist this type can also be used when we need to include a file as a part of an HTTP response I will create three endpoints and name the methods according to the file types the first one will be for a physical file the second one for a virtual file and the last one for the file content type if we access the first endpoint or we will download the file the physical file result method needs two things an absolute path to the file on the server as the first parameter in our case it's a PDF file on my computer and the file tab as the second parameter which we declare as PDF if you need to work with various file tabs you can look up mime types to declare them or check the link I have provided below the video to use the end point with the feature of result we should remember the lesson about the static W folder to access a static folder like w root we need to register the use static files method after registering the use static file method we should add the W root folder to the project additionally I will copy and paste the PDF file into the W root folder with the w folder declared we can easily specify a relative path to the file along with its file name if we navigate to the correct URL we can successfully access the file and when you open the developer tools you will see a representation of the file once more add these highlights the difference between virtual and physical files virtual files need a relative path and can be sourced from the W root folder while physical files require an absolute path to a file on the server the last file tab is a file content result which is declared using a byte array this code is plenty sharp when we access the endpoint the server provides the same file to us before we conclude this lesson this is one more approach to make this code cleaner we can use the file and physical file methods provided by the controller base abstract class this will make your code much cleaner and more concise and as always listen assignments at the conclusion of each lesson I highly encourage you to complete the assignments as they will greatly contribute to your progress in ESPN network 7. by consistently practicing you will see faster results in your Learning Journey and the assignments answers you can download from the GitHub the link is below thanks for watching if you have any questions or need further assistance feel free to comment below don't forget to hit this like button and subscribe to our channel for more great coding content stay updated with the latest videos by ringing the notification Bell happy Korean
Info
Channel: hikitoc
Views: 669
Rating: undefined out of 5
Keywords:
Id: UUmrfjpIpCI
Channel Id: undefined
Length: 9min 48sec (588 seconds)
Published: Sun Sep 03 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.