try to call from view in "Transaction" controller, The other "CreditCard" controller:
#(Url.Action("ShowImage", "CreditCard"))/" + ConcatString
from src propery of IMG tag.
but because it from other controller the URL is invalid.
Insted Of:
/creditcard/showimage/45809014157220320
Its:
/Transaction/TransactionToPDF/creditcard/showimage/45809014157220320
Wild guess, your Action doesn't accept a null for the image id? Rather than trying to add concatstring try specifying the parameter e.g.,
#Url.Action("ShowImage", "CreditCard", new { ImageId= ConcatString })
Again, depending on what the allowed parameters are for the Action this should let the correct URL be resolved by the routing system
Of course you can use Html.Action helper. But it is easier for me to do it like this.
<img src="/CreditCard/ShowImage/#ConcatString" class="img float-xs-right" width="350" height="380" id="creditCardImage" />
I think you have an "Area" issue. If they are in different areas, you should be able to do this:
#(Url.Action("ShowImage", "CreditCard", new { area = "" }))/" + ConcatString
Related
I can't find out how to solve this. I have two URLs. These are /my-url-1 and /my-url-2. Both going to different views.
The thing is that I have an ActionLink on /my-url-1's view which should make /my-url-2 and go to that view.
The problem is that ActionLink makes /my-url-1/my-url-2 as the URL and not just /my-url-2.
I was searching two days about how to fix it but couldn't find anything.
PD: I'm not using Areas so please don't tell me that I just should put the "area" parameter as a "".
These are two urls which goes to different controllers and different actions.
View which has the ActionLink (URL:/my-url-1) :
<div class="btn-index-container">
#Html.ActionLink("Url 2", "MyAction", "MyController")
</div>
This ActionLink should render:
Url 2
But it's rendering:
Url 2
where /my-url-1 is my current URL
Route Config
routes.MapRoute(
name: "route1",
url: "my-url-2", //without parameters
defaults: new { controller = "MyController", action = "MyAction" },
);
routes.MapRoute(
name: "route2",
url: "my-url-1", //without parameters too
defaults: new { controller = "MyController2", action = "MyAction2" }
);
So, when I go to localhost:port/my-url-1 it loads MyAction2 which renders a view. This view has inside an ActionLink(described above) which should render a /my-url-2 link.
Well, I've worked inside the MVC framework and I could told you about how Url.RouteUrl or Html.RouteLink works. At the end, the method which create the URL is GetVirtualPathForArea (this method is called before UrlUtil.GenerateClientUrl, which receive the VirtualPathData.cs created by GetVirtualPathForArea, as a parameter) from System.Web.Routing.RouteCollection.cs.
Here I left a link to the MVC source code:
https://github.com/aspnet/AspNetWebStack/blob/master/src/System.Web.Mvc
I found that, my Request.ApplicationPath was changing when I loaded /my-url-1. It was crazy because the application path was /.
At last, the problem was that the /my-url-1 was pointing to a virtual directory created on the IIS some time ago by error.
To know where your IIS configuration file is, please follow the link below:
remove virtual directory in IIS Express created in error
The solution was remove the .vs directory (which contains the config .vs\config\applicationhost.config) and rebuild
I think most of the Helpers that render URLs works more or less in the same way, so I hope it'll useful for all of you!
In your case, maybe no its necesary, just pass the parameters with null values, E.G.
#Html.ActionLink("EspaƱol", null, null, new { Lng = "es" }, null)
In this way, the parameters change, and the view is relative, depending on where you are.
I've created a route with template "/documentation/{category?}/{feature?}" and named it docu, but I get an error when I try to use it in the anchor tag helper.
Link:
<a asp-route="docu" asp-route-category="layout" asp-route-feature="colors" asp-page-handler="Feature" class="link">Color</a>
Error:
InvalidOperationException: Cannot determine the 'href' attribute for
. The following attributes are mutually exclusive: asp-route
asp-controller, asp-action asp-page, asp-page-handler
It works if I use #Url.RouteUrl() in the cshtml file, but I don't know if I get access to it in a tag helper.
Any advice?
Like the exception tells you, the main problem here is that you're simultaneously using both asp-page-handler and asp-route. The first is for generating a URL to a Razor Page, while the latter is for generating a URL to a named route. The two are mutually exclusive, so you simply need to pick one and remove the other.
Color
You are missing the last part of the asp-rout-color="docu"
I ended up with usin the IUrlHelper in a custom tag helper instead of using the anchor helper.
services.AddScoped<IUrlHelper>(x =>
{
var actionContext = x.GetRequiredService<IActionContextAccessor>().ActionContext;
var factory = x.GetRequiredService<IUrlHelperFactory>();
return factory.GetUrlHelper(actionContext);
});
I am new in mvc and I have a situation where I am convinced that I am mapping a route correctly although it is not.
it is a very basic login form with the option of passing in parameters.
this is the html
<li>Login</li>
and this is the action method in the 'Home' controller
public ViewResult LoginForm(string userName)
{
return View();
}
This is how is my attempt at mapping the route
routes.MapRoute(
null,
"Login/{userName}",
new { controller = "Home ", action = "LoginForm", UrlParameter.Optional }
);
The url is however displaying as follow
/Home/LoginForm?loginUser=user
my aim would be the following
Login/user
Advice perhaps as to why it is not mapping correctly. I have already registered a number of routes in the Global.asax.cs file. Could it have something to do with the order with which they were registered?
Try this:
<li>Login</li>
change the parameter loginUser to userName.
Use userName instead of loginUser
<li><a href='#Url.Action("LoginForm", "Home", new {userName="user"})'>Login</a></li>
You are hitting a different address than the one specified in MapRoute. The mapped route will not fire. Change both the parameter and the action name.
<li>Login</li>
You need to access /Home/Login not /Home/LoginForm. The routing is done automatically if the right address is accessed.
EDIT:
Following your address edit:
As far as I know, you cannot generate a link such as Login/{userName} using Url.Action; if you don't specify a controller, this defaults to Home controller
You can however access the Login/{userName} link directly from the browser (due to the mapped route)
You can create a "static" (i.e. classic) link, passing a hard-coded address:
<li>Login</li>
Please note that the userName added/removed per JavaScript.
K... I'm doing something obviously wrong. I have a simple page with a file input control on and a submit button. I'm trying out the new "File" ActionResult that was released with the Mvc RC...
All, I want to happen is when the submit button is clicked the selected file is uploaded to the database. This all works fine...
Then, after the page refreshes I want a image to display the resulting image that was uploaded. The issue, is that the image is not rendering... I get the broken image...
This is the portion that is getting the file and sending it back to the view...
var a = Helper.Service.GetAttachmentById(id, MembershipProvider.SecurityTicket);
if (a == null)
{
return View(new ImagePlaceHolderViewData(new { Id = id }));
}
return View(new ImagePlaceHolderViewData(new { Id = a.Id, Image = a, FileContent = File(a.Data, a.ContentType) }));
Then in the view I have a image tag like so...
<img src="<%=Model.FileContent.FileContents %>" />
I have also tried...
<img src="<%=Model.FileContent%>" />
Thoughts..??
FileResult returns the ASCII or binary contents of the file. When you say do the following:
<img src="<%=Model.FileContent.FileContents %>" />
You are attempting to push the binary image data into the src attribute. That will never work, because the src must a URL or a path to an image.
There are several ways of doing what you want, and the most correct solution in this case, would be to create a new controller that returns the binary data like you are attempting, and then you set the src attribute to be the path to correct action on your new controller. E.g:
<img src="/image/result/12345" />
This points to the following (really simple and incomplete) example controller:
public class ImageController : Controller
{
public ActionResult Result(int resultID)
{
// Do stuff here...
return File(..);
}
}
Note that the name I chose for the action is most likely not any good, but it serves its purpose as an example. Hope this was helpful.
I think it's pretty simple: the src attribute of your img tag requires an URL. What you're doing here is just putting the FileStream object in there (which implicitly calls the ToString method on that object). Your resulting html is probably something like this:
<img src="FileStream#1" />
Did you check your html source?
What you probably should do is provide a method which returns the data, and pass the route to that to your view. Your resulting html 'should' then look something like this:
<img src="/Images/View/1" />
So, steps you have to do are:
Create a method on your controller that returns a FileContentResult
Pass your image ID to your view
Use Url.RouteUrl to generate an url which you can put in your img tag, which points to the method returning your image data.
There's also another solution:
<img src="data:image/gif;base64,<%=Convert.ToBase64(Model.FileContent)%>"/>
which works in some browsers, and doesn't in some browsers(IE! of course). But the stable solution is what Thomas J mentioned.
I want to embed a link to a controller action in my page so I can use it from javascript. Something like
var pollAction = '/Mycontroller/CheckStatus'
Now I am happy to hardcode it, but it would be really nice if there were a method I could use to create the URL. The AjaxHelper/HtmlExtensions contain methods to create hyperlinks (.ActionLink(...) and so on), but if you look into the guts of them, they rely on a method called UrlHelper.GenerateUrl() to resolve a controller and action into a url. This is internal so I can't really get at this.
Anyone found a good method in the framework to do this? Or must I roll my own?
Have you tried something along these lines?
var pollAction = '<%=Url.Action("CheckStatus", "MyController") %>';
If your page or control inherits from ViewPage or ViewUserControl, use the Url.Action method.
If not, use this instead:
String url = RouteTable.Routes.GetVirtualPath
(
((MvcHandler) HttpContext.Current.CurrentHandler).RequestContext,
new RouteValueDictionary
(
new
{
controller = "MyController",
action = "CheckState",
id = idParameter
}
)
).VirtualPath;
Place this inside a method on your code-behind and call it from the HTML view.