How do I create a MVC Razor template for DisplayFor()

OK, I found it and it’s actually very simple. In my Views\Shared\DisplayTemplates folder I have Reading.cshtml containing the following: @model System.Int32 <span id=”@ViewData.ModelMetadata.PropertyName”>@Model</span> This renders the correct tag using the name of the property as the id attribute and the value of the property as the contents: <span id=”Reading”>1234</span> In the view file this can … Read more

How do I set the default namespaces in MapHttpRoute?

We had this problem with the Umbraco core so we created our own IHttpControllerSelector, the source code can be found here: https://github.com/WebApiContrib/WebAPIContrib/blob/master/src/WebApiContrib/Selectors/NamespaceHttpControllerSelector.cs You can also install nuget package WebAPIContrib which contains NamespaceHttpControllerSelector. To register this you can do this on app startup: GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(GlobalConfiguration.Configuration)); The implementation is pretty straight forward and only deals with … Read more

ASP.NET MVC – How to call void controller method without leaving the view?

Basically @Html.ActionLink() or <a></a> tag uses get request to locate the page. Hence whenever you clicked it, you request to your AddToCart action method in ProductController and if that action method returns null or void so a blank or empty page is shown as you experienced (because or @Html.ActionLink() get request by Default). So if … Read more

Migrate Global.asax to Startup.cs

As you are already aware, OwinContext consumed by Startup.Configuration() is different from the traditional ASP.NET HttpContext consumed by MvcApplication.Application_Start(). Both are using different context pipelines. More specifically, ASP.NET MVC still relies on System.Web.dll while ASP.NET Web API doesn’t. Therefore, based on your code, some methods usually laid in MvcApplication.Application_Start() can’t be run within Startup.Configuration(): AreaRegistration.RegisterAllAreas();: … Read more

Use of AuthConfig, BundleConfig, FilterConfig , RouteConfig and WebApiConfig in App_Start() folder in MVC

App_Start is just another folder that groups together ASP.NET MVC configuration, which in previous versions of ASP.NET MVC was done in Global.asax. ASP.NET MVC introduces more and more configuration elements, and this folder is ideal to place this configuration. For example, MVC 5’s new auth. configuration, such as for third-party login providers, are also placed … Read more

Bootstrap Icons not showing in published ASP.NET MVC application

If your bundling your scripts and CSS then assets like images may not be found. In your BundleConfig.cs file, create your bundle with CssRewriteUrlTransform: bundles.Add(new StyleBundle(“~/Content/css/bootstrap”).Include(“~/Content/bootstrap.min.css”, new CssRewriteUrlTransform())); Include in _Layout.cshtml: @Styles.Render(“~/Content/css/bootstrap”) Everything should be good. And yes, viewing what’s happening over the network to see what URL is generating the 404s will help. EDIT: … Read more

Get list of users with assigned roles in asp.net identity 2.0

Not an expert, but … There seemed to be no built in funcionality for this in Identity and I could not get it work from built in Roles also (it seems to not work with claims based Identity). So I ended up doing something like this: var users = context.Users .Where(x => x.Roles.Select(y => y.Id).Contains(roleId)) … Read more