July 08, 2024

JaiHoDevs

What are the different types of methods in asp.net Core

In ASP.NET Core, the structure and types of methods are quite similar to those in ASP.NET MVC but with some enhancements and modernizations. Here are the different types of methods commonly used in ASP.NET Core:

1. Standard Action Methods

These methods correspond to standard HTTP verbs and are used to handle incoming requests.

HttpGet

Handles GET requests, typically used to retrieve data and display views.

[HttpGet]

public IActionResult Index()

{

    return View();

}


HttpPost

Handles POST requests, typically used to submit data to the server.

[HttpPost]

public IActionResult Create(MyModel model)

{

    if (ModelState.IsValid)

    {

        // Process data

        return RedirectToAction("Index");

    }

    return View(model);

}


HttpPut

Handles PUT requests, typically used to update existing data.

[HttpPut]

public IActionResult Update(int id, MyModel model)

{

    if (ModelState.IsValid)

    {

        // Update data

        return RedirectToAction("Index");

    }

    return View(model);

}


HttpDelete

Handles DELETE requests, typically used to delete data.

[HttpDelete]

public IActionResult Delete(int id)

{

    // Delete data

    return RedirectToAction("Index");

}


2. JsonResult Methods

These methods return JSON-formatted data, useful for AJAX calls.

public JsonResult GetJsonData()

{

    var data = new { Name = "John", Age = 30 };

    return Json(data);

}


3. ContentResult Methods

These methods return raw content, such as strings or HTML.

public ContentResult GetContent()

{

    return Content("Hello, World!");

}

What are the different types of methods in asp.net Core


4. FileResult Methods

These methods return files to the client, such as images or documents.

public FileResult GetFile()

{

    byte[] fileBytes = System.IO.File.ReadAllBytes(@"path\to\file.pdf");

    return File(fileBytes, "application/pdf", "file.pdf");

}


5. PartialViewResult Methods

These methods return partial views, which are fragments of HTML intended to be included in other views.

public PartialViewResult GetPartialView()

{

    return PartialView("_PartialViewName");

}


6. RedirectResult Methods

These methods redirect the client to a different URL.

Redirect

Redirects to a specified URL.


public RedirectResult RedirectToGoogle()

{

    return Redirect("https://www.google.com");

}


RedirectToAction

Redirects to another action within the same or different controller.

public RedirectToActionResult RedirectToIndex()

{

    return RedirectToAction("Index");

}


RedirectToRoute

Redirects to a specific route.

public RedirectToRouteResult RedirectToSpecificRoute()

{

    return RedirectToRoute(new { controller = "Home", action = "Index" });

}


7. ViewResult Methods

These methods return views to the client.

public ViewResult GetView()

{

    return View();

}


8. StatusCodeResult Methods

These methods return HTTP status codes, useful for signaling errors.

public StatusCodeResult UnauthorizedAccess()

{

    return StatusCode(401);

}


9. Specialized Action Methods with Attributes

ActionName

Allows specifying a different action name than the method name.

[ActionName("MySpecialAction")]

public IActionResult SpecialAction()

{

    return View();

}


NonAction

Indicates that a method is not an action method.

[NonAction]

public void HelperMethod()

{

    // This is not an action method

}


10. Async Action Methods

ASP.NET Core supports asynchronous action methods for better performance.

public async Task<IActionResult> GetDataAsync()

{

    var data = await _service.GetDataAsync();

    return Json(data);

}


11. Custom Method Filters

ASP.NET Core allows for creating custom filters to handle actions pre- and post-execution.

public class CustomActionFilter : IActionFilter

{

    public void OnActionExecuting(ActionExecutingContext context)

    {

        // Code to execute before the action

    }


    public void OnActionExecuted(ActionExecutedContext context)

    {

        // Code to execute after the action

    }

}


These various types of methods and attributes in ASP.NET Core provide a comprehensive set of tools for handling HTTP requests and responses effectively and efficiently.

Subscribe to get more Posts :