Output caching with location=server not working - asp.net-mvc-4

I have implemented outputcaching on Action method of controller with
[OutputCache(Duration = 60, VaryByParam = "ID", VaryByHeader="User-Agent", Location = System.Web.UI.OutputCacheLocation.Server)]
public ActionResult MyAction(int ID)
{
_logger.Error("call-" + ID);
}
and I have made log entry within this action. So, My problem is Output caching with location=Server calls this action method every time. It should not call every-time.
So, what's the problem with this?
When I changed Caching location i.e location=Client then only first time it calls the method. Then afterwards action method not called.

Are you trying output caching on the ChildAction? I know you have not written ChildAction attribute in your question but just asking for confirmation ? If it is then you can't use output caching on which are not Child Actions. You need to use this caching plugin if you want to use output caching without child action.

Related

Dynamic model attribute with SAILSJS

My use case needs me to use Dynamic Model attributes with my Sails.Js project and all of my methods fail. First I tried to call model1 method from model2 but it doesn't works. I also tried to use global variables but this fails too because, the global is set after the load of the application, and finaly I called model1Controller from the model2 but it still not working.
This is the mothod in my model2Controller
getbyTable:function(req,res){
let field = Field.find({fTable:req.param('tableName')});
field.exec(function(err, response) {
if(err) {
res.status(500);
return res.view('500', {data: err});
}
//res.json(response);
return response;
});
},
And this is my model2
'use strict';
const fieldController = require('../controllers/Model1Controller');
const donnees = fieldController.getbyTable('person');
module.exports={
}
When run it fires as error that req.param is not a function.
Need help!!! Thanks.
This looks like an interesting way to organize - I'd be curious to see how successful a project could be without Model attributes set in stone.
I can see a few specific errors in your code, maybe seeing them will help you move forward.
From your model2 you invoke getbyTable('person') with a string argument - but all controller methods are really intended to have reqest/response arguments (as in the definition getbyTable(req, res). Directly invoking a controller method is a bit unusual - if you ever did it, I'd expect to pass along a request and response from some other controller method.
Inside getbyTable, you try to return the response, but the response is only defined inside the exec callback. The response would be an array of objects fetched from your database - if that's what you need, you can't return them directly since database calls are async.
Your controller should probably do something with res in all logical branches, such as res.json, res.send, etc.
It's a bit hard to see exactly what you're trying to achieve, so maybe if you could explain the goal I could have some more relevant advice. For now, I could say that you might want to consider putting some code inside a service rather than a controller. Create file /api/services/MyService.js and use it to export a method that can be called from your model2 (you can require the service just like the controller). Keep the controller methods strictly for handling requests.
Good luck.

Change AR session config after fragment created

I want to re-configure AR session after session has been running. I want to change Augmented Images database.
I don't seem to find a way to set the reset the session configuration.
getSessionConfiguration(Session session)
This function only seems to be called once at the beginning.
Is there a way to re-configure? Should I not be using the fragment?
I make changes to my config on the fly. You can accomplish this by extending ARFragment and having access to the config or simply be accessing the ARFragment from your xml within your activity. Here is an example.
arSceneView.session?.apply {
val changedConfig = config
changedConfig.planeFindingMode = Config.PlaneFindingMode.HORIZONTAL_AND_VERTICAL
configure(changedConfig)
}
That's it, just call configure(myNewConfig) and it will update it for you.
Of course in this example, I get the current config, modify it and put it back, but you could replace it if preferred.

Save or Merge Patch Entity

I want following functionality in spring data rest.
If I post to a collection resource end point, server should check if the object exists. if it exists already it should perform the same functionality as it does with merge-patch on item resource. If object does not exist already it should create it.
Is this achievable in spring data rest. If so then how?
If it is possible in your use case, you might want to use PUT instead of POST, as PUT should work as you expected.
Solution with POST
You can achieve the desired behavior with Spring Data REST Event handlers.
Just create a Handler method which accepts your entity and annotate it with #HandleBeforeCreate. In this method, you can implement your behavior, i.e. check if the object exists and update it manually or just do nothing and let the Spring Data REST handle the entity creation.
#RepositoryEventHandler
public class EntityEventHandler {
#Autowired
private EntityService entityService;
#HandleBeforeCreate
public void handleEntityCreate(Entity e) {
if (entityService.exists(e)) {
entityService.update(e);
}
}
}
EDIT:
I just realized that you would also need to stop the create event after your update. You might try throwing a custom Exception and Handling it to return 200 and the updated entity.

Ninject Authorization vs Action Filter binding order

I have 2 filters I'm trying to apply using Ninject's BindFilter<> syntax, and they are being applied with their dependency injection successfully. The problem lies in the fact that the one sorts out who the current user is and binds that InRequestScope, and has to run before the second one when checking for maintenance authorization - otherwise it doesn't know which user you're referring to.
The filter binding in NinjectWebCommon.cs looks like this:
kernel.BindFilter<CurrentUserFilter>(FilterScope.Global, 0).InRequestScope();
kernel.BindFilter<SetupRightsAttribute>(FilterScope.Controller, 1).WhenControllerType<MaintenanceController>().InRequestScope();
So in the Maintenance controller, I'm wanting the first one to run and then the second; in everything else just the first one - and this works, the second one is only used when browsing to something in my Maintenance controller.
The 2 filters are declared as such (irrelevant details excluded)
public class CurrentUserFilter : IActionFilter
{
public void OnActionExecuting(ActionExecutingContext filterContext)
// implementation - breakpoint 1
}
public class SetupRightsAttribute : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
// implementation - breakpoint 2
}
I'm expecting breakpoint 1 to be hit and then breakpoint 2, but for some reason it's always breakpoint 2 first - and because it doesn't know the user at this point it tells me I have no authorization.
I have tried many different permutations of FilterScope and order settings, even binding them in a different order, but nothing works...what am I doing wrong?
Authorization filters are executed before action filters, because authorization is earlier in the MVC execution pipeline than processing the action.

Ninject: More than one matching bindings are available

I have a dependency with parameters constructor. When I call the action more than 1x, it show this error:
Error activating IValidationPurchaseService
More than one matching bindings are available.
Activation path:
1) Request for IValidationPurchaseService
Suggestions:
1) Ensure that you have defined a binding for IValidationPurchaseService only once.
public ActionResult Detalhes(string regionUrl, string discountUrl, DetalhesModel detalhesModel)
{
var validationPurchaseDTO = new ValidationPurchaseDTO {...}
KernelFactory.Kernel.Bind<IValidationPurchaseService>().To<ValidationPurchaseService>()
.WithConstructorArgument("validationPurchaseDTO", validationPurchaseDTO)
.WithConstructorArgument("confirmPayment", true);
this.ValidationPurchaseService = KernelFactory.Kernel.Get<IValidationPurchaseService>();
...
}
I'm not sure what are you trying to achieve by the code you cited. The error is raised because you bind the same service more than once, so when you are trying to resolve it it can't choose one (identical) binding over another. This is not how DI Container is supposed to be operated. In your example you are not getting advantage of your DI at all. You can replace your code:
KernelFactory.Kernel.Bind<IValidationPurchaseService>().To<ValidationPurchaseService>()
.WithConstructorArgument("validationPurchaseDTO", validationPurchaseDTO)
.WithConstructorArgument("confirmPayment", true);
this.ValidationPurchaseService = KernelFactory.Kernel.Get<IValidationPurchaseService>();
With this:
this.ValidationPurchaseService = new ValidationPurchaseService(validationPurchaseDTO:validationPurchaseDTO, confirmPayment:true)
If you could explain what you are trying to achieve by using ninject in this scenario the community will be able to assist further.
Your KernelFactory probably returns the same kernel (singleton) on each successive call to the controller. Which is why you add a similar binding every time you hit the URL that activates this controller. So it probably works the first time and starts failing after the second time.