Session Mocking-Rhino Mocks - rhino-mocks

I want to mock the following in my action controller using RhinoMocks
Session["myId"] = someGuid.ToString();
How Do I do that?
Thank you

Download MVC Contrib from Codeplex http://mvccontrib.codeplex.com/ or using NUget. Add the references to your .net project and follow the example below:
[TestMethod]
public void MyController()
{
//Mocks your controller with the session
TestControllerBuilder builder = new TestControllerBuilder();
HomeController controller = new HomeController();
builder.InitializeController(controller);
ViewResult result = controller.MyController() as ViewResult;
Assert.IsNotNull(result);
}

Related

.Net 6.0 AuthorizationContext.Controller equivalent

I'm migrating some old ASP.NET MVC 5 code to .NET 6.0 and having some trouble with an AuthorizationFilter that, within its OnAuthorization implementation, accesses the decorated method's controller class instance, like this:
// Get the decorated method's name
string actionName = filterContext.ActionDescriptor.ActionName;
// Get the controller instance and then, its type
Type controllerType = filterContext.Controller.GetType();
I just canĀ“t see how I would get the controller instance (or event the method's name) from the AuthorizationFilterContext available in .NET 6.0. Any help?
create new class
public class LogEntry : IAuthorizationFilter
{
public void OnAuthorization(AuthorizationFilterContext context)
{
var controllerActionDescriptor = context.ActionDescriptor as
ControllerActionDescriptor;
string controllerName =
controllerActionDescriptor?.ControllerName;
string actionName =
controllerActionDescriptor?.ActionName;
}
}
In Program.cs register LogEntry class
builder.Services.AddScoped<LogEntry>();
on controller action method
[ServiceFilter(typeof(LogEntry))]
public IActionResult Index()
{
return View();
}

How to find controller name in ASP.NET Core

How to find the controller name in ASP.NET Core?
I googled it, very first I got the solution ControllerContext.ActionDescriptor.ControllerName.
But in my ASP.NET Core application, ControllerContext is not showing any properties.
Please help me.
If you're using ASP.NET Core 3+, you can utilize endpoint routing and get the current endpoint inside a controller action.
Controller actions have a ControllerActionDescriptor metadata, which provides you info about the action.
[ApiController]
[Route("")]
public class HelloController : ControllerBase
{
[HttpGet]
public async Task<IActionResult> Index(CancellationToken cancellationToken)
{
var endpoint = HttpContext.GetEndpoint();
var actionDescriptor = endpoint!.Metadata.OfType<ControllerActionDescriptor>().First()!;
var controllerName = actionDescriptor.ControllerName;
// -> gives you "Hello"
return Ok(controllerName);
}
}
If you're looking for the fully-qualified name, you can use the controller type info
var controllerFullName = actionDescriptor.ControllerTypeInfo.FullName;
// -> gives you "ApiPlayground.HelloController"

Alternative to Cache Insert fuction in Net framework

I am trying to set up this code https://stackoverflow.blog/2008/07/18/easy-background-tasks-in-aspnet/ (this code should be able to cyclically run some procedure on the same time - for example I need to update a database).
But I am currently using the ASP.NET Core 3.1 and there is no Insert function to the cache. I have managed other things around (for example other name of the delegate) but there is no chance with this issue.
Unfortunately I am still a begginer in C# so some things are still hard for me. I would appreciate any advice. Thank you a lot!
ASP.NET Core supports several different caches. The simplest cache (In-memory cache) is based on the IMemoryCache. By using the cache's Set method, you could create and add a new entry to the cache.
To use the Memory cache in asp.net core, you could refer the following steps:
First, register the MemoryCache in the ConfigureServices method of Startup class:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddMemoryCache();
}
Then, refer the following code to use it:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
namespace MemoryCacheApplication.Controllers
{
public class HomeController : Controller
{
private readonly IMemoryCache memoryCache;
public HomeController(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
}
public IActionResult Index()
{
DateTime currentTime;
bool isExist = memoryCache.TryGetValue("CacheTime", out currentTime);
if (!isExist)
{
currentTime = DateTime.Now;
var cacheEntryOptions = new MemoryCacheEntryOptions()
.SetSlidingExpiration(TimeSpan.FromSeconds(30));
memoryCache.Set("CacheTime", currentTime, cacheEntryOptions);
}
return View(currentTime);
}
}
More detail information, please check the Cache in-memory in ASP.NET Core

How to use DataTokens in ASP.NET Core 3.1 when performing dynamic routing?

I am using the class DynamicRouteValueTransformer built into ASP.NET Core 3.1 to perform dynamic controller routing as demonstrated in the code below.
public class UrlRouteTransformer : DynamicRouteValueTransformer
{
public override ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
{
var controller = "home";
var action = "index";
//Make decision here on which controller and action to call. i.e call the database etc
values["controller"] = controller;
values["action"] = action;
return new ValueTask<RouteValueDictionary>(values);
}
}
This code is working as expected, but I want to know how you would set data tokens?

Ninject for Asp.net Web API

I got this error when using Ninject with Web API, but it works with MVC Controller:
Type 'App.Web.Controllers.ProductController' does not have a default constructor
NinjectControllerFactory :
public class NinjectControllerFactory : DefaultControllerFactory
{
private IKernel ninjectKernel;
public NinjectControllerFactory()
{
ninjectKernel = new StandardKernel();
AddBindings();
}
protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
{
return controllerType == null ? null : (IController)ninjectKernel.Get(controllerType);
}
public void AddBindings()
{
ninjectKernel.Bind<IProductRepository>().To<EFProductRepository>();
}
}
Global.asax.cs :
BundleConfig.RegisterBundles(BundleTable.Bundles);
ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory());
ProductController :
public class ProductController : ApiController
{
private IProductRepository repository;
public ProductController(IProductRepository ProducteRepository)
{
this.repository = ProductRepository;
}
public IEnumerable<Product> GetAllProducts()
{
return repository.Products.AsEnumerable();
}
}
You have overriden the DefaultControllerFactory. But this is used to instantiate ASP.NET MVC controllers (one deriving from System.Web.Mvc.Controller). It has strictly nothing to do with ASP.NET Web API controllers (the ones deriving from System.Web.Http.ApiController).
So basically what you have done here is dependency injection into ASP.NET MVC. If you want to use this for the Web API you may take a look at the following guides:
http://www.strathweb.com/2012/05/using-ninject-with-the-latest-asp-net-web-api-source/
http://www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
You should use the latest Ninject Web API package, which solves these problems already. See here: http://nuget.org/packages/Ninject.Web.WebApi.WebHost/
You need to set DependencyResolver property of the HttpConfiguration. What you have done was for ASP.NET MVC and not ASP.NET Web API.
So get the NuGet package and set DependencyResolver:
var kernel = new StandardKernel();
// use kernel to register your dependencies
var dependencyResolver = new NInjectResolver(kernel);
config.DependencyResolver = dependencyResolver; // config is an instance of HttpConfiguration based on your hosting scenario