Giter Club home page Giter Club logo

asp.net-mvc-lambda-expression-helpers's Introduction

Hi, I am Ivaylo Kenov! ๐Ÿ‘‹

๐Ÿ”ฅ CEO @ AMBITIONED
๐Ÿ’ป CTO @ SoftUni
๐Ÿคฏ 20x Founder
๐Ÿ˜Ž C-level in 4 Successful Startups
๐Ÿค“ Technical Trainer with 1000+ Lectures
โ˜„๏ธ Experienced C# & ASP.NET Developer
๐Ÿ™ Open-Source Enthusiast
โœ”๏ธ Creator of My Tested ASP.NET
โœŒ๏ธ C# Teacher @ YouTube

YouTube Channel Views

Nuget Nuget

Facebook Instagram LinkedIn

GitHub Stats

GitHub Stats

GitHub Trophies

GitHub Trophies

asp.net-mvc-lambda-expression-helpers's People

Contributors

ivaylokenov avatar vladislav-karamfilov avatar waynebrantley avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

asp.net-mvc-lambda-expression-helpers's Issues

Same action name update issue

I tried to use expression helper to create and update, create is working fine but in edit, I have two actions with the same name Edit, I cant able to create BeginForm helper for edit.
Please guide me.

 public ActionResult Edit(int? id){
     Employee employee = db.Employees.Find(id);
     if (employee == null){
        return HttpNotFound();
     }
     return View(employee);
  }
  
  [HttpPost]
  public ActionResult Edit(Employee employee){
     if (ModelState.IsValid){
        db.Entry(employee).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
     }
     return View(employee);
  }

Attribute routing

Is this supposed to work with attribute routing?
added [RouteArea] at the top of HomeController (not passing anything so it would be the root page).
added [Route("somepage"] to a somepage action on the controller.
Used your library to generate a url to HomeController.SomePage() and it generated Home\SomePage instead of \SomePage

So, I was not sure if this just generated default routes or was supposed to also use attribute routing?

Assuming Assembly reference when using System.Web.Mvc 5.2.4.0 - 5.2.6.0

After upgrading to a newer version of System.Web.Mvc a green squiggly shows under HTML helper methods such as the following when System.Web.Mvc.Expressions is referenced :

    @using (Html.BeginForm()
    {
    }

On investigation it says that it is assuming reference 5.2.3.0 and has found 5.2.4.0

By no means a major bug but it is annoying.

Get string outside of controller/url/helper/etc

There are places where we need help generating URLs outside of a controller.
For example, when setting up special routes:

routes.MapRoute("health", "health", "/Health");  

Would be nice to do something like this:

routes.MapRoute("health", "health", System.Web.Mvc.Expressions.Url<HomeController>(c=>c.Health());  

can not support in razor?

i want to use it in CSHTML
something like:

<li>@Html.ActionLink<HomeController>(c => c.Index(5))</li>

RouteValueDictionary support

Your methods like RedirectToActionPermanent, Url.Action, etc. all take an optional object for route values.
This works most of the time.

However, if you pass in an actual ``RouteValueDictonary' to the methods they do not work. The standard MVC library code that takes this kind of value does work.

Url.Action<SomeController>(c=>c.SomeMethod(), someRouteValueDictionary) should work.

You could create overloads that accept the RouteValueDictionary and act appropriately:

        public static string Action<TController>(this UrlHelper url, Expression<Action<TController>> action, RouteValueDictionary routeValues) where TController : Controller
        {
            RouteInformation routeInformation = FromExpression<TController>(action, routeValues);
            return url.Action(routeInformation.ActionName, routeInformation.ControllerName, routeInformation.RouteValueDictionary);
        }

        public static string Action<TController>(this UrlHelper url, Expression<Func<TController, Task>> action, RouteValueDictionary routeValues) where TController : Controller
        {
            RouteInformation routeInformation = FromExpression<TController>(action, routeValues);
            return url.Action(routeInformation.ActionName, routeInformation.ControllerName, routeInformation.RouteValueDictionary);
        }

        public static RouteInformation FromExpression<TController>(LambdaExpression action, RouteValueDictionary routeValueDictionary)
                   where TController : Controller
        {
            string actionName = action.GetActionName();

            var controllerType = typeof(TController);
            string controllerName = controllerType.GetControllerName();

            routeValueDictionary.ProcessParameters(action);
            routeValueDictionary.ProcessArea(controllerType);
            var routeInformation = new RouteInformation(actionName, controllerName, routeValueDictionary);
            return routeInformation;
        }

That would be a lot of overloads.

Or you could create a shared routine that creates the initial route value dictionary correctly, something like

private RouteValueDictionary GetDictionaryFromObject( object routeValues){
   if (routeValues is RouteValueDictionary)
     return (RouteValueDictionary)routeValues;
  return new RouteValueDictionary(routeValues);

Optionally, Microsoft evidently uses TypeHelper.ObjectToDictionary() so potentially you could use that too?

Area is not calculated correctly

public static void ProcessArea(this RouteValueDictionary routeValues, Type targetControllerType)
{
var areaName = targetControllerType.GetAreaName() ?? string.Empty;
routeValues.AddOrUpdateRouteValue("area", areaName);
}

Async actions

public async Task<ActionResult> Index()
{ 
  if (await someAsyncCode())
     return this.RedirectToAction(c=>c.Index());   //<-- this says code needs to be awaited
  return View(); 
}

How do you handle async actions?
The above code gives compiler/editor warnings that the expression c->Index() needs awaited.
Of course awaiting it and making it async means your signaturres wont work.

Performance

Is there a performance hit in using this library?

I was looking at the code and it appears to do a lot of reflection every HTTP call. Reflection is an intensive task.

I was wondering if any performance testing has been performed in using this method.

I really love it BTW, hate magic strings!

Attribute Routing Area Not working

https://github.com/ivaylokenov/ASP.NET-MVC-Lambda-Expression-Helpers/blob/master/System.Web.Mvc.Expressions/Internals/MvcExtensions.cs#L44

At the line above, when your code tries to locate the RouteArea attribute, it incorrectly uses RoutePrefix.

The documentation for RoutePrefix says it defaults to the 'AreaName' and if that is blank it gets the area name from the namespace. Nothing could be further from the truth! The code is here:
http://sourcebrowser.io/Browse/ASP-NET-MVC/aspnetwebstack/src/System.Web.Mvc/RouteAreaAttribute.cs

As you can see, it does nothing.
The fix is easy...the line should be
return routeAreaAttribute.AreaName;

I just now encountered this as very few controllers we have use AttributeRouting.

asp.net core

Do you have the equivalent library for use in asp.net core? (Full framework or a particular standard is fine)

RouteValueDictionary Extension

Several things we use want a RouteValueDictionary, so we need these additional extensions.
I could have added these extensions, but you made the RouteInformation class internal. Any chance you could remove that modifier?

public static RouteValueDictionary RouteValueDictionary<TController>(
               this HtmlHelper helper,
               Expression<Func<TController, Task>> action,
               object routeValues = null)
           where TController : Controller
       {
           var routeInfo = RouteInformation.FromExpression<TController>(action, routeValues);
           return routeInfo.RouteValueDictionary;
       }

Thoughts?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.