How to create ASP.NET Web API Url?

The ApiController has a property called Url which is of type System.Web.Http.Routing.UrlHelper which allows you to construct urls for api controllers. Example: public class ValuesController : ApiController { // GET /api/values public IEnumerable<string> Get() { // returns /api/values/123 string url = Url.Route(“DefaultApi”, new { controller = “values”, id = “123” }); return new string[] { … Read more

ASP.NET MVC 4 Custom Authorize Attribute with Permission Codes (without roles)

I could do this with a custom attribute as follows. [AuthorizeUser(AccessLevel = “Create”)] public ActionResult CreateNewInvoice() { //… return View(); } Custom Attribute class as follows. public class AuthorizeUserAttribute : AuthorizeAttribute { // Custom property public string AccessLevel { get; set; } protected override bool AuthorizeCore(HttpContextBase httpContext) { var isAuthorized = base.AuthorizeCore(httpContext); if (!isAuthorized) { … Read more

Anti forgery token is meant for user “” but the current user is “username”

This is happening because the anti-forgery token embeds the username of the user as part of the encrypted token for better validation. When you first call the @Html.AntiForgeryToken() the user is not logged in so the token will have an empty string for the username, after the user logs in, if you do not replace … Read more

Why use @Scripts.Render(“~/bundles/jquery”)

Bundling is all about compressing several JavaScript or stylesheets files without any formatting (also referred as minified) into a single file for saving bandwith and number of requests to load a page. As example you could create your own bundle: bundles.Add(New ScriptBundle(“https://stackoverflow.com/questions/12192646/~/bundles/mybundle”).Include( “~/Resources/Core/Javascripts/jquery-1.7.1.min.js”, “~/Resources/Core/Javascripts/jquery-ui-1.8.16.min.js”, “~/Resources/Core/Javascripts/jquery.validate.min.js”, “~/Resources/Core/Javascripts/jquery.validate.unobtrusive.min.js”, “~/Resources/Core/Javascripts/jquery.unobtrusive-ajax.min.js”, “~/Resources/Core/Javascripts/jquery-ui-timepicker-addon.js”)) And render it like this: @Scripts.Render(“https://stackoverflow.com/questions/12192646/~/bundles/mybundle”) One … Read more

How to add reference to System.Web.Optimization for MVC-3-converted-to-4 app

Update Version 1.1.x is available, read the release notes: https://www.nuget.org/packages/Microsoft.AspNet.Web.Optimization The Microsoft.Web.Optimization package is now obsolete. With ASP.NET (MVC) 4 and higher you should install the Microsoft ASP.NET Web Optimization Framework: Install the package from nuget: Install-Package Microsoft.AspNet.Web.Optimization Create and configure bundle(s) in App_Start\BundleConfig.cs: public class BundleConfig { public static void RegisterBundles(BundleCollection bundles) { bundles.Add(new … Read more