I'm having a problem using a jQuery plugin in an ASP.NET MVC Project, where images are being searched for in locations that don't exist.
I am receiving missing image errors like such:
GET http://localhost:51710/Home/img/chesspieces/wikipedia/bN.png 404 (Not Found)
But I have no idea why it seems to be searching for images inside of localhost/Home like that?
I want to search inside a content folder for the images of course, (e.g. \Content\chessboard.js\img), not a view/view controller like localhost/Home.
Why is this, and how can I achieve what I want?
My routeconfig file is still in it's default state:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
Edit:
Here's the line in chessboard.js, where I can change the route after localhost/Home, but not localhost/Home itself:
cfg.pieceTheme = 'img/chesspieces/wikipedia/{piece}.png';
Use absolute path:
cfg.pieceTheme = '/img/chesspieces/wikipedia/{piece}.png'; // note the '/'
There is a very helpful Url extension to point to appropriate location of your content files (like images):
#Url.Content
Which makes it possible to get your files from appropriate content directories.
#Url.Content("~/Content/chessboard.js/img")
In case there is no possibility to use Razor syntax inside this file you could try this:
cfg.pieceTheme = "~/Content/chessboard.js/img/chesspieces/wikipedia/{piece}.png';
Related
There is an application on asp.net mvc. And such an interesting moment. There is a start page which is the default configuration in the route. And when you start the application, it writes only localhost ... How to ensure that the default page prescribe its full path like everyone else, i.e. localhost / Controller / Action.
Tell me how to achieve this? I nned like:
How can I achieve this?
My RouteConfig:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Authorization", action = "Index", id = UrlParameter.Optional }
);
}
The entire point of the "default" route is having something to show the user when no route is filled in. People that will visit your site won't type in or find "www.yoursite.com/authorization/index" so that's why the routing configures a default route.
If you want your page to show something different by default you need to change the data in the default route in your routes.config and create a controller for it. You could also let the default land at an action that only redirects to "authorization/index" which will change the URL.
Lastly you could simply fix it with some javascript on the page, but I think this will also cause a page reload so fixing it server side is probably best.
I'm currently facing an issue with an MVC application I'm busy with, when testing the site in a local environment it shows the login page like it's supposed to as specified in the RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Login",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id = UrlParameter.Optional }
);
}
But as soon as this gets uploaded the server it only renders the _layout.cshtml.
I have also attempted to specify the page in the application properties but this has not worked either.
Any advice or things to try would be greatly appreciated and thank you in advance.
Please note, when routing to the controller and views manually it all works fine, it's just the initial load of the site.
In my MVC 4 web appication if I access the Home page I call the following url:
sitename/Home
I now added a subfolder called Mobile to the Controllers folder.How can I configure routing to be able to call the Home controller in the Mobile folder like this
sitename/Mobile/Home
Here's my RegisterRoutes method:
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Test.Controllers" }
);
routes.MapRoute(
name: "Mobile",
url: "Mobile/{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
namespaces: new string[] { "Test.Controllers.Mobile" }
);
}
ASP.NET MVC routes are order dependant and should go in order of most specific to most generic.
At the moment, if you type the url Mobile/Home/Index the routing will try and map that to:
controller: Mobile
action: Home
id: Index
using the default route and never get to your intended route map.
If you swap the MapRoute declarations around, then MVC will see that it starts with "Mobile" and use that route as intended.
#dav_i is 100% correct in that the route needs to be before the other one. The URL for mobile has Mobile in the URL, so you would have to have Mobile in any action links or redirects... So ideally, in your global.asax, you'd try to detect whether the browser is mobile, and redirect within there... But you still have to manage having Mobile in the URL. That's because functions like Url.Action and Html.ActionLink expect the controller and action to be the full URL, and this:
#Url.Action("Action", "Control")
produces:
/Control/Action
And as such, you'd have to handle this. Alternatively, you could use mobile views without having to worry about this. See this tutorial.
I don't know what happened to my website. From today the default action "Index" in only one controller doesn't work anymore.
If I call http://website.com/Valuation i get 403 error page because the webserver doesn't route my request and try to browse the folder. If I write http://website.com/Valuation/Index everything works.
I search in all the code but i can't find the problem, everything seems fine like other controllers.
How can i find the problem? Do you know if there are a known issue that cause that problem or you know if there is a trace\log\debugger of routing requests?
Thanks
Mic
Most probably the issue is you have a folder by name Valuation in your website root. Thats why the valuation index action is not working. Instead of routing to the Controller Action the url http://website.com/Valuation is routing to the Folder Valuation.
Delete this folder Valuation from your root or rename it then this url http://website.com/Valuation will work.
Also check if the ValuationController has the public ActionResult Index() ([HttpGet] method.
Check ~\App_Start\RouteConfig.cs file
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional}
);
}
}
This is a very strange problem. I am new to the mvc world coming from web forms and i am trying to understand its concepts. Using the MVC template in vs 2013 (premium), I have built a project. In order to see how things work:
I create a new controller 'IndexController' and put it in the folder .../Controllers/IndexController.cs
I create a new View for this controller 'Index.cshtml' and put it in the corresponding folder: .../Views/Index/[#] Index.cshtml
Then I change the routing so that the default routing will now point to this IndexController and not to the default HomeController
Here is my routing table:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "GetIndexPage", id = UrlParameter.Optional }
);
}
}
You can see that I am using 'GetIndexPage' as the default action instead of 'Index' (I'm playing around and see how it works)
Whenerver I make a change in the Index.cshtml (say I add a simple markup and hit 'Run' I always receive the error message
Server Error in '/' Application. The resource cannot be found.
Looking at the address bar I see that the browser is looking for the resource 'localhost:xxx/Index/Index' instead of 'localhost:xxx/Index/GetIndexPage'. To solve this problem, I go in the IndexController and put a breakpoint inthe line
return View(...);
Now I hit 'Run', after stopping at the breakpoint, every thing works perfectly. So it is not a problem of routing since the page is displayed after this breakpoint trick. Visual Studio seems to mess up with the deployment to the IIS Express I am using after I have made a change to the cshtml view. The problem does not occur when I make a change in the code behind of the controller. I don't know where to look at...I have spent this whole night trying to find a solution in Google and stackoverflow...I don't want to reinstall the whole visual studio. Any help, any hint to a certain direction will be greatly appreciated.
After a lot of trials I discover that mvc framework wants the action and the view (that this action returns) must have the same name. In my case this is what has worked:
route configuration:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Index", action = "IndexPage", id = UrlParameter.Optional }
);
IndexController:
public class IndexController : Controller
{
public ActionResult IndexPage()
{
return View();
}
}
You can see that route action has same name with method of controller: IndexPage
And finally create a view with name IndexPage.cshtml
With this configuration when I make a change in the cshtml file it is immediately reflected in the browser and there is not the error reported above.
I just want to have a confirmation if this is indeed the way things must be set up with the mvc approach. (thanks with your help)