I'm using mvc.jquery.datatables with Asp.Net MVC 4 and DataTables
It works fine and as expected.
When I introduce T4 MVC to replace MVC magic strings, T4 MVC does not like the return type that mvc.jquery.datatables wants me to use in my Ajax action method.
The return type is:
DataTablesResult<MyType>
T4 MVC does not like this and responds with the error:
The type 'Mvc.JQuery.Datatables.DataTablesResult <TSource> ' has no constructors defined T4MVC.cs
So, how can I get T4 MVC and mvc.jquery.datatables to play ball, and at worst, can I get T4 MVC to ignore that one action?
I don't think there is a great way to ignore specific actions, though it would make sense to add that.
I think that if you were to add a [Obsolete] attribute on it, it would get ignored, though clearly that's a hack. If you look in ProcessControllerActionMethods in t4mvc.tt, you'll see the various conditions that cause actions to get ignored.
If you want to tackle creating a new attribute to ignore actions, we can take this to https://t4mvc.codeplex.com/. Or potentially it can be smarter and auto detect this situation.
Related
I am trying to use Microsoft.AspNetCore.OData, v7.0.0.0-beta1, in a simple project.
I am failing to filter, select, and use top or skip.
Overriding the ValidateQuery of the EnableQueryAttribute, I can successfully perform these type of operations so I believe the library is buggy.
I could not find the right channel to report the issue, can anyone help?
The sample code to reproduce is available here: https://github.com/norcino/AspNetCoreSamples/tree/master/SampleODataApp
The solution to the problem was the actual initialization of the MVC route builder.
Like in the .net framework version it is possible to specify which operation is allowed for OData query. In the example below I am whitelisting everything but you can do a more fine tuning passing an instance of QueryOptionSetting.
app.UseMvc(routeBuilder =>
{
routeBuilder
.Select()
.Expand()
.Filter()
.OrderBy(QueryOptionSetting.Allowed)
.MaxTop(2000)
.Count();
routeBuilder.EnableDependencyInjection();
});
Note that, the attribute [HttpGet, EnableQuery(AllowedOrderByProperties = "Id")], will effectively enforce the order by to the sole Id property specified in the attribute, but first you need to enable all from the configuration.
I am evaluating the configuration of a Web API App with Asp.Net Core 2. To configure I know it is better for my project to use .AddMvcCore() rather then use AddMvc() since I don't need Razor as we can see here.
Now, I am not sure if I also need this .AddFormatterMappings(). So my question is what is it for?
You can see from the source code in the MVC GitHub repo that it adds a FormatFilter service into the DI setup. The summary for the FormatFilter class is as follows:
A filter that will use the format value in the route data or query
string to set the content type on an ObjectResult returned from an
action.
I am developing a single page application in ASP.NET MVC. The ASP.NET Framework version is 4.5 and the MVC version is 4. I am wondering if ViewComponents in ASP.NET Core might help me with my problem.
The Problem
The application has a single "proper" Index view and multiple child/nested partial views inside it that depend on some parent views' data, for example:
Index > GridPartial --> RowInfoPartial > AdditionalRowInfoPartial
where > denotes nesting, i.e. GridPartial is nested inside Index and AdditionalRowInfoPartial is nested inside RowInfoPartial; and --> denotes dependency injection, i.e. both RowInfoPartial and AdditionalIRowInfoPartial depend on some data from GridPartial.
Ideally, I would like to only show what I need to show, but unfortunately, it is not the case right now. All the nested partial views are rendered synchronously when the application is loaded, meaning loading the application is extremely slow. Moreover, because of dependencies, many partial views have to call their corresponding actions in the controller to pass their dependencies to business logic and show the correct data on the client, which slows the loading time even further.
The Solution (?)
I've read up on ViewComponents in ASP.NET Core, and they seem to satisfy all my needs:
They can be invoked asynchronously, to reduce loading time and only show views when needed
Supports controller dependency injection
Supports business logic
I want to make sure that ViewComponents do indeed help me solve my problem before I migrate to ASP.NET Core. Is my understanding of this technology correct, or is there some other way of solving my problem without having to migrate to ASP.NET Core?
I'm trying to build an asp.net mvc 4 application.
I want the application to encompass both a HTML site and a restful api, e.g.
www.mysite.com/MyDetails/
www.mysiste.com/api/users/{userid}/Details/
In the above example I would use 2 controller classes.
MyDetailsController which inherits from System.Web.Mvc.Controller
DetailsController which inherits from System.Web.Http.ApiController
I've also added a simple 'Users Route' to the WebApiConfig:
routeTemplate: "api/users/{userid}/{controller}/{id}
In my early testing it appears as though the following scenarios are invalid:
www.mysite.com/api/users/12345/MyDetails/
www.mysite.com/Details/
Both of those return a 404.
This is definitely a good thing but what I'm trying to find out is why doesn't it work?
Can I rely on it not working or is it just coincidence in my simple test?
I've read about people struggling to develop a single MVC app/project that encompasses both HTML and REST apis but the most common complaint seems to be you can't duplicate controller names and it still seems like you can't simply use a namespace to differentiate them.
In this example I've deliberately designed the class names to avoid any conflict so what other gotchas are waiting to trip me up?
Thanks,
Chris A
Check your routes file, should be Global.asax under RegisterRoutes. The MapRoute call should tell you everything you need to know for MVC routing. Keep in mind, the order of the routes is important: top routes take priority over the bottom. Web API uses the WebApiConfig class and MapHttpRoute call to configure routes.
Please ensure you have put a (MVC) route on top of the action you wish to hit of your controller, default action being index.
[System.Web.Mvc.Route("Help")]
public ActionResult Index()
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
}
Code above will hit this action method (provided your controller is registered and derived either from apiController or Controller) in the following way:
http://localhost:54541/help inside your IISExpress.
To register please do the following:
In "global.asax.cs", you’ll need to add:
AreaRegistration.RegisterAllAreas();
I have a custom authorization attribute, required only for some actions, which checks the request headers for a custom token. The token is checked in a database. Checking the database requires access to a service which I would like to have injected through the constructor.
The way I have read this can be done (here, here, and here) is by having a constructor-less filter and injecting the dependent one like this:
kernel.BindFilter<MyAuthorizeFilter>(FilterScope.Controller, 0).WhenControllerHas<MyAuthorizeAttribute>();
However the BindFilter method is not available to me as I have setup Ninject as described here. This is using Ninject.Web.Common instead of Ninject MVC3 as I read that Ninject MVC3 would not work with MVC4 RC. How else can I go about accomplishing this?
I have read also that I could add to GlobalFilters.Filters - however I don't want it to be present on every action.
Thanks in advance for any answers.
I'm not completely sure I see how you have set up your application, but my experience has been that if you want a filter for a WebApi controller you need to add it to the HttpFilterCollection that is available from the GlobalConfiguration.Filters. This is a Different set of filters than what MVC uses (through the GlobalFilterCollection).