0

i have a simple test controller defined as

[Route("~/testAction/testcontroller/{viewhash}")]
public ActionResult ATest(string viewHash)
        {
            var viewname = commonDataCollection.Instance.WidgetHashList[viewHash];
            return Content($"Success hash by view name is: {viewhash}","text/html");
        }

this works if i call this from browser http://localhost:49359/testAction/testcontroller/FuMmWA1217dgnaT

the below works if i use actual action/controller name instead of attribute route value but if i try to do the same with Html Helper with routing defined such as :

@Html.Action("testAction", new { viewhash = "FuMmWA1217dgnaT",area="",controller= "testcontroller" });

or

@Html.Partial("testAction", new { viewhash = "FuMmWA1217dgnaT",area="",controller= "testcontroller" });

or even when i am trying the render functions, i am getting following error:

Execution of the child request failed. Please examine the InnerException for more information. at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func1 func) at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.HttpServerUtility.ExecuteInternal(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage, VirtualPath path, VirtualPath filePath, String physPath, Exception error, String queryStringOverride) at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm, Boolean setPreviousPage) at System.Web.HttpServerUtility.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) at System.Web.HttpServerUtilityWrapper.Execute(IHttpHandler handler, TextWriter writer, Boolean preserveForm) at System.Web.Mvc.Html.ChildActionExtensions.ActionHelper(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues, TextWriter textWriter) at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, String controllerName, RouteValueDictionary routeValues) at System.Web.Mvc.Html.ChildActionExtensions.Action(HtmlHelper htmlHelper, String actionName, Object routeValues) at ASP._Page_Views_widgetrenders_contact_cshtml.Execute() in D:\Projects\company projects\CMSAlpha\CMSAlpha\Views\widgetrenders\contact.cshtml:line 20 The controller for path '/2/abc' was not found or does not implement IController. System.Web.HttpException (0x80004005): The controller for path '/2/abc' was not found or does not implement IController. at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClass7.<BeginProcessRequest>b__6() at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func1 func) at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerAsyncWrapper.<>c__DisplayClass7.b__6() at System.Web.Mvc.HttpHandlerUtil.ServerExecuteHttpHandlerWrapper.Wrap[TResult](Func`1 func)

Alok
  • 808
  • 13
  • 40
  • Pretty sure that what you're trying to do can't be done. Html.Action expects the name of the action method in your controller. Not anything to do with routes. – Tobias Sep 04 '19 at 20:07
  • @Tobias is there anyway to initiate this using route values, should i look for extension method approach... i must have this functionality to call right controller and get response. – Alok Sep 04 '19 at 20:19
  • @Tobias https://stackoverflow.com/questions/30382953/html-renderaction-with-route-name according to this i think its pretty much possible, not sure why mine is causing issues – Alok Sep 04 '19 at 20:27
  • What in that suggests that it's possible? – Gabriel Luci Sep 04 '19 at 20:34
  • 1
    If you're going to hard code the route (which is what your doing if you use part of the route there) then there is no point in using `Html.Action` at all. Just hard code the whole URL. The whole point of using `Html.Action` is so that you can change the route in one place and not have to update it everywhere it is used. – Gabriel Luci Sep 04 '19 at 20:37
  • @GabrielLuci got it, but my problem is i want to hide the controller method name and only expose the friendly route to whoever is writing the view, now i can grab the data using ajax... but we want to render it server side due to project flow requires as such, if an extension method can do this lookup i am all upto it – Alok Sep 04 '19 at 20:46
  • How can you hide the controller method name from someone who is writing code in the same project? – Gabriel Luci Sep 04 '19 at 20:50
  • its an abstract system using different projects as plugins in IoC, so although the other users/developer can look through public methods using intellisense they dont know where to look for if we dont tell them – Alok Sep 04 '19 at 20:52
  • If they can use `Html.Action` then intellisense will help them find the name of the controller action. – Gabriel Luci Sep 04 '19 at 20:59
  • Mate, you are trying to do the very opposite of what you should. Exactly as pointed by @GabrielLuci, you completely missed the reason why these helper exist. And the linked question works only because the method name is the same as word used in the route. – Al Kepp Sep 04 '19 at 21:45
  • @AlKepp thanks for yours and Gabriel's comments, i think its not possible without hiding behind many methods so if i come up with something i will post solution here in future. – Alok Sep 05 '19 at 08:21

0 Answers0