views:

63

answers:

3

Hi there,

I'm pretty new to ASP.NET MVC and I hope it is not a too silly question. So here it comes.

I have ...

My problem...

  • the routing of my application (it worked fine without using a subfolder after the domain-name). The applications homepage loads not to bad, with css files but without ressources like images (defined in css files) and without jQuery ajax calls similar to /mycontroller/myaction
  • links are only working once (the second time I get a page similar to this link: http://mydomain/mysubfoler1/myappfolder/myController/myController/myAction)
  • jquery ajax calls are not working. (I have additional js-files in the script folder that are calling several controller actions. p.e.


$.getJSON("../mycontroller/myaction/" + id, function(figure) {
    // do something
    });



Here's my Global.asax contaning the routing:

public static void RegisterRoutes(RouteCollection routes)

{

    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(    
        "Default",
        "{controller}/{action}/{id}",
        new { controller = "myController", action = "Index", id = "" }
        defaults    
    );

    routes.MapRoute(    
        "Root",
        "",
        new { controller = "myController", action = "Index", id = "" }   
    );    
}

protected void Application_Start()    
{    
    ViewEngines.Engines.Clear();
    ViewEngines.Engines.Add(new MyApplicationWeb.LocalizationWebFormViewEngine());
    RegisterRoutes(RouteTable.Routes);
    //RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}

Any suggestions? My first suggestion was to use areas like:

"mysubfolder1/myappfolder/{controller}/{action}/{id}"

(but without any luck)

Thank you very much for your help!

+1  A: 

I had similar issues with content not loading up correctly and using the UrlHelper class was able to fix my problems. This helper will generate proper urls for your content, links, etc.

<%=Url.Content("~/Content/Images/image.jpg")%>
<%=Url.Action("MyAction")%>
Mark
+1  A: 

Every request to your application is going to be split up into URL tokens between the slash characters. The route "{controller}/{action}/{id}" will match the first three segments of the URL after the domain.

Thus, adding a subfolder will break your application, because the subfolder will be interpreted as the controller argument. When you say you used ""mysubfolder1/myappfolder/{controller}/{action}/{id}" without any luck, what do you mean? This would be a simple way to go that should work for you.

When you say your links aren't working, I'm guessing that you're probably not generating them properly. You should use the Url.Action and Url.Content methods to properly generate links inside your application - this prevents them from breaking when you move folders and routing tables around.

womp
+1  A: 

...css files but without ressources like images (defined in css files) and without jQuery ajax calls similar to /mycontroller/myaction

You need to edit any and all jQuery/CSS image links or URLs. ASP.NET MVC routing only works for incoming web requests, and since your css files are static, any images referenced using background-image: url('../images/background.png') will be relative to the folder in which the css file is.

As other answers state, in any View (.ascx and .aspx files), use <%= Url.Action("ActionName") %> to generate proper URLs based on your routing. Similarly, use <%= Url.Content("~/folder/something.png") %> to generate links for anything on your web server (images, css, js etc.).

But, you can't use these in .css and .js files because they are not parsed using the ASP.NET WebForms view engine.


UPDATE as per comment:

Since ASP.NET is not responsible for serving static files like .css and .js, you will need to hard code the URLS for actions/controllers in the files.

An alternative, but possibly messy, approach, is to place strings inside your .css/.js files that can be easily distinguished (example: {someAction}) instead of the actual URL, then make any .css or .js file link point to an action on a controller. The action would then take the appropriate file, replace all the strings in it with the appropriate URLs, then output it back to the user. You would then use the following to serve up the files

return Content(fileText, "text/css"); // or "text/javascript"

Another method, is to place any css/js in a separate view (with no master page), which enables you to use Html and Url Helpers. You can then serve those files by simply returning the appropriate view with the proper code. Note: make sure you set the proper ContentType, see http://stackoverflow.com/questions/971806/asp-net-mvc-and-text-xml-content-type/971986#971986

Baddie
Thank you! Using the Url-Helper Methods helped a lot. But how about handling the js-files? Example: jquery calling controller action: $.getJSON("/myController/myAction/1", { ajax: 'true' }, function(figure) { .... Any suggestions?
hkda150
Answer updated.
Baddie
Thanks a lot. App is working correctly now.
hkda150