0

Hi i am trying to add "@Url.Action" in c#.net (WebForms) to resolve the path conflict in server and with my local system using configuration file. i had this line and in this i am trying to use url.action in href property.

<a href="/User/Add"><span class="glyphicon glyphicon-plus"></span>&nbsp;Add User</a>

my question is how to use this url.action method .......

i tried like

href="@Url.Action("Add","User")"

but its not accepting this.... thank you..

SamGhatak
  • 1,487
  • 1
  • 16
  • 27
thiru
  • 173
  • 1
  • 4
  • 16
  • 3
    seems okay for Razor syntax, what do you mean by "not accepting"? – Ian Feb 22 '16 at 08:20
  • @htiru Ignore the Error and try to run/build your project. – Waqar Ahmed Feb 22 '16 at 08:20
  • what error do you get ? do you try it? – Uthman Rahimi Feb 22 '16 at 08:31
  • the error is ** 404 ** and the url is ** http://localhost:7469/Home/@Url.Action(%22Add%22,%20%22User%22) ** – thiru Feb 22 '16 at 09:12
  • Like Ian said, what do you mean by "not accepting" ? Because C#.Net doesn't provide this extension of code alone. The Razor Engine must be used with. Do you have the right packages included in your project ? – D4rkTiger Feb 22 '16 at 09:12
  • so do i need to install RazorEngine from nuget manager ?? @D4rkTiger – thiru Feb 22 '16 at 09:22
  • Yes absolutly, if you don't have the Razor Mvc packages to solve Razor extensions Visual Studio couldn't solve references ;-) – D4rkTiger Feb 22 '16 at 09:26
  • i have added the razorengine nuget to my packages and now can i use the same syntax like href='@Url.Action('Add','User)''.. or need any other syntax.. – thiru Feb 22 '16 at 09:34
  • Make sure you have an action Add() inside UserController, and you have created a view with name add or you must have any other view which is being return by Add() action. – Pawan Feb 22 '16 at 09:37
  • its not mvc i am doing in instead c#@Pawan – thiru Feb 22 '16 at 09:38
  • Sorry, I was not aware of this. – Pawan Feb 22 '16 at 09:43
  • Normally you can use it like you have written @htiru – D4rkTiger Feb 22 '16 at 10:08
  • guys, read the question, this guys does not use .NET MVC, he does use .NET WebForms. Adding Razor is just one part of the problem. – Anton Feb 22 '16 at 10:09
  • @htiru, please see my answer and mark it as correct if you think it answered your question. by doing so you help the future visitors of stackoverflow checking your question. – Anton Feb 22 '16 at 13:32

8 Answers8

3

As far as I am aware, Url.Action is intended only for .NET MVC, not for .NET WebForms - there are no actions and no controllers in the WebForms.

Moreover, from your comments it seems like the terms C# and MVC should be clarified. C# is a programming language. You can use C# with both frameworks .NET MVC and .NET WebForms. You can use C# in both the .cshtml files (thanks to Razor view engine) and the ASPX pages.

Anton
  • 2,458
  • 2
  • 18
  • 30
2

use the following:

<%=new System.Web.Mvc.UrlHelper(this.Request.RequestContext).Action("Index", "DetailedConferenceReport") %>

Or

Just use this

<a class="btn btn-warning " href="../DetailedConferenceReport/Index">                                    
    <i class="fa fa-backward"></i>
</a>
smolchanovsky
  • 1,775
  • 2
  • 15
  • 29
0

This think works for me, I used a link this way

<a href="@Url.Action("LogOut", "Account")">hi </a>

And this is working for me. Can you copy the html from source of the page and verify?

SamGhatak
  • 1,487
  • 1
  • 16
  • 27
  • this might works in mvc(cshtml) but what about in c#(.aspx) – thiru Feb 22 '16 at 09:14
  • I don't think you can do it in a WebForms project. The '@Url.Action()' method is for MVC only. The parameters are Controller and Action. You have to do it as you mentioned in the question.This [link](http://stackoverflow.com/questions/4931869/calling-asp-net-mvc-controllers-from-normal-aspx-web-form) might be helpful. – SamGhatak Feb 22 '16 at 10:04
0

I think you may have a problem with the " quotes try using <a href='@Url.Action("Add", "User")'><span class="glyphicon glyphicon-plus"></span>&nbsp;Add User</a>

DaveHutchy
  • 57
  • 6
  • still doesn't work @daveHutchy. i am trying this in c#(.aspx) – thiru Feb 22 '16 at 09:13
  • Ah right, then i believe as @Anton has mentioned that in .aspx (Web Forms) that is not available and will only work for .NET MVC projects. However i am pretty sure that you could use a Jquery Ajax call to this Method depending on the context you are using it. – DaveHutchy Feb 24 '16 at 10:02
0

Normally you just have to add

Microsoft.AspNet.Razor

which by dependency includes all others it needs

D4rkTiger
  • 352
  • 1
  • 13
0

We can use ResolveUrl instead .. That's work fine in c#

thiru
  • 173
  • 1
  • 4
  • 16
0

U can use <%=Url.Action("index", "Home")%> in Asp.net Webform and @Url.Action("index", "Home") in Asp.net MVC

KKDev
  • 141
  • 1
  • 3
  • 17
-1

Because it seems you're using .aspx and not .cshtml ("Razor"), you'd need to use

<a href="<%= Url.Action("Add","User") %>"> ... </a>

Note: This is for an MVC application

You can check ASP.NET "special" tags for a quick run-down for the .aspx tags, and https://msdn.microsoft.com/en-us/library/fy30at8h(v=vs.100).aspx for full MSDN documentation on .aspx syntax.

Community
  • 1
  • 1